Several Custom Fields in a same container
- This topic has 2 replies, 2 voices, and was last updated 3 years, 1 month ago by
AnLip.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
April 1, 2022 at 11:48 PM #35414
AnLip
ParticipantHi !
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 } ?>
April 2, 2022 at 6:56 PM #35426Long Nguyen
ModeratorHi,
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/
April 5, 2022 at 6:25 PM #35473AnLip
ParticipantHi Long,
That worked, thanks a lot.
PHP is black magic to me, sorry.
-
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.