Home › Forums › General Issues › Echo Taxonomy field IDs to WP Query
I’m trying to echo out the value of my Taxonomy field to a WP Query on the same page. I have:
<?php
$cats = get_field('rel_posts_cat');
$my_query = new WP_Query(array(
'showposts' => '10',
'category__and' => array($cats),
));
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<?php the_title() ?>
<?php endwhile;?>
But nothing displays, even though I have selected a few categories in the CMS. Also, if I add these manually i.e.
'category__and' => array('1,12'),
Then that also works.
I’m presuming I’m doing something very silly!
Thanks in advance for anybody’s help.
Assuming the $cats
is receiving values it seems like you’ll want to update the code to:
<?php
$cats = get_field('rel_posts_cat');
$my_query = new WP_Query(array(
'showposts' => '10',
'category__and' => $cats,
));
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
The change is on the 'category__and'
parameter, since $cats should be returning an array assuming you’ve defined the Return Value
to Term ID
The topic ‘Echo Taxonomy field IDs to WP Query’ is closed to new replies.
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.