Home › Forums › General Issues › Show Custom Field results from one post type in template of another › Reply To: Show Custom Field results from one post type in template of another
Here’s a step-by-step.
1. Find a way to get the artist’s name. It’s stored as the post’s title. Great. We can grab it with the_title()
.
2. Find where the artist is declared in the ‘release’ CPT. It’s inside a custom field named album_artist
. Custom fields are stored inside meta keys.
3. Construct a query with these values and spit out the results. You get:
<?php
$args = array(
'numberposts'=> -1, // Fetch all posts...
'post_type'=> 'release', // from the 'release' CPT...
'meta_key' => 'album_artist', // which inside this meta key...
'meta_value' => the_title() // has this meta value.
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) :
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_field('album_title') ?></li>
<?php
endwhile; echo '</ul>';
endif; wp_reset_query(); ?>
Hope that helped.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.