Support

Account

Home Forums General Issues Help with Taxonomy Field Querying Multiple Taxonomy Terms

Solved

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(); ?>
  • Hi @vitalunfolding

    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(); ?>
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Help with Taxonomy Field Querying Multiple Taxonomy Terms’ is closed to new replies.