Home › Forums › General Issues › Help with Taxonomy Field Querying Multiple Taxonomy Terms
Admittedly I’m not great with PHP but I’m attempting to use the Taxonomy Field to generate an array of Tag Term IDs which can be queried to display a list of posts pulled from multiple Tags. My code is only pulling posts from the first term id in the array and I’m not sure what I’ve missed. Any help would be greatly appreciated.
<?php
$postnumber = get_field('number_of_posts');
//custom field for posts per page
$selected = get_field('selected_tags');
// ACF Taxonomy Field (Return Value = Term ID)
$selectags = implode(",",$selected);
// Separating the Term IDs with commas
?>
<?php $args = array(
'posts_per_page' => $postnumber,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish',
'tag__in' => array($selectags)
// PROBLEM: Only pulls the first Term ID in the array. What gives?
); ?>
<?php query_posts($args); if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink() ?>">read the post</a>
<p><?php the_tags('Tags: ', ', ', ''); ?></p>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
Haven’t had a chance to replicate the issue above but as an initial measure try changing array($selectags)
to just $selectags in your tags__in parameter. If ACF is already returning an array of tag IDs (which is then stored as such in you $selectags variable) then you shouldn’t need to warp it in another array.
Hope that helps-if not, let me know if not and I’ll try and replicate the issue
Cheers
Thanks so much for the reply. I was making this way more complicated than it needed to be. The Taxonomy Field:
$selected = get_field('selected_tags');
was already returning an array of IDs making my variable:
$selectags = implode(",",$selected);
completely unnecessary. Working code is below. Hopefully it’ll save somebody else some time & trouble.
<?php
$postnumber = get_field('number_of_posts');
//custom field for posts per page
$selected = get_field('selected_tags');
// ACF Taxonomy Field (Return Value = Term ID)
?>
<?php $args = array(
'posts_per_page' => $postnumber,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish',
'tag__in' => $selected
); ?>
<?php query_posts($args); if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink() ?>">read the post</a>
<p><?php the_tags('Tags: ', ', ', ''); ?></p>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
You must be logged in to reply to this topic.
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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.