Several Custom Fields in a same container

Support General Several Custom Fields in a same containerResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35414
    AnLipAnLip
    Participant

    Hi !

    I am struggling to show the two custom fields along with the post title. Post title shows but not the "custom_field_1" nor the "custom_field_2". Any idea why ? The query works fine (showing terms grouped by a custom field value "date" and ordered by name in "DESC").

    The code :

    <?php
    // get the terms, ordered by name
    // https://developer.wordpress.org/reference/functions/get_terms/
    // https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
    $taxonomy = 'date-exposition';
    $tax_terms = get_terms( 
      array(
        'taxonomy' => $taxonomy,
        'hide_empty' => false, // change to true if you don't want empty terms
        'orderby' => 'name',
        'order' => 'DESC',
        'fields' => 'names', // return the term names only
      )
    );
    
    foreach($tax_terms as $tax_term) { // loop through the terms
      echo '<h2>' . $tax_term . '</h2>'; // echo the term name as a h2
      $term_posts = get_posts( // find posts with the correct term
        array(
          'no_found_rows' => true, // for performance
          'ignore_sticky_posts' => true, // for performance
          'post_type' => 'exposition',
          'posts_per_page' => -1, // return all results
          'tax_query' => array( // https://developer.wordpress.org/reference/classes/wp_tax_query/
            array(
                  'taxonomy' => $taxonomy,
                  'field'    => 'name',
                  'terms'    => array( $tax_term )
              )
          ),
          'fields' => 'ids', // return the post IDs only
        )
      );
    	
    echo '<ul class="fm-expo-list">'; // open bullet list
    foreach ($term_posts as $term_post_id) { // loop through posts
    $post_title = get_the_title($term_post_id); // get post title
    $post_permalink = get_the_permalink($term_post_id); // get post link
    $custom_field_1 = rwmb_get_value($fm_expo_city); // get custom post 1
    $custom_field_2 = rwmb_get_value($fm_expo_year); // get custom post 2
    echo
    '<li>
    <div class="fm-expo-line">
    <span class="fm-expo-title">' . $post_title . '</span>
    <span class="fm-expo-city">' . $custom_field_1 . '</span>
    <span class="fm-expo-year">' . $custom_field_2 . '</span>
    </div>
    </li>';
    }
    echo '</ul>'; // close bullet list
    }
    ?>
    #35426
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I do not see the custom field ID in your code, just variable so please add the real field ID and pass the post ID to the helper function and re-check it.

    $custom_field_1 = rwmb_get_value( 'custom_field_1_id', '', $term_post_id); // get custom post 1
    $custom_field_2 = rwmb_get_value( 'custom_field_2_id', '', $term_post_id); // get custom post 2

    Refer to the documentation https://docs.metabox.io/rwmb-get-value/

    #35473
    AnLipAnLip
    Participant

    Hi Long,

    That worked, thanks a lot.

    PHP is black magic to me, sorry.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.