Home › Forums › General Issues › Query posts via Taxonomy field › Reply To: Query posts via Taxonomy field
So what you need to do is get the ACF field, which will return an array of term IDs and use that array to do a WP_Query, similar to what magicstick posted.
For this you need to set the taxonomy field on the book edit page to return Term ID, if it’s returning Term Object it will be a bit more complicated.
$topics = get_field('topics');
if ($topics) {
if (!is_array($topics)) {
$topics = array($topics);
}
$args = array(
'post_type' => 'quotes',
'posts_per_page' => 5,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'topics',
'terms' => $topics,
),
),
);
$quote_query = new WP_Query($args);
if ($quote_query->have_posts()) {
while($quote_query->have_posts()) {
$quote_query->the_post();
echo '<p>',get_the_content(),'</p>';
}
}
wp_reset_postdata();
}
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.