WordPress.org

Support

Support » How-To and Troubleshooting » Does get_posts() support tax_query?

Does get_posts() support tax_query?

  • The codex says that get_posts uses all the same arguments as query_posts, so why doesn’t the tax_query work with get_posts?

    $myquery['tax_query'] = array(
    	array(
    		'taxonomy' => 'portfolio-tags',
    		'terms' => array('bacon'),
    		'field' => 'slug',
    	),
    );
    $myposts = get_posts( $myquery );
    
    if ($myposts) :
      echo "bacon here!"
    else:
      echo "no freaking bacon";
    endif;

    echos out “no freaking bacon” which is obviously not the end result i want, but was a quick way to test whether it was pulling any data.

    but if i change $myposts to

    $myposts = query_posts( $myquery );

    then i get a result.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I got mine to work:

    $args = array(	'numberposts'=>-1,
    		'order_by'=>'menu_order',
    		'order'=>'ASC',
    		'post_type'=>'book',
    		'exclude'=>$the_id,
    		'tax_query'=>array(array('taxonomy'=>'series',
    					'field'=>'slug',
    					'terms'=>$slug
    					))
    		);
    $series_posts = get_posts($args);

    The only difference I can see is that my “terms” isn’t in an array. That really shouldn’t make a difference, though, since it’s definitely supposed to work with arrays.

    thanks! actually the difference is that you’ve defined the post type and the taxonomy i was trying to pull from only applied to a specific post type. apparently get_posts() only searches posts by default unless you define another post type.

    Ah, I didn’t realize you were (also) working with an alternate post type.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Does get_posts() support tax_query?’ is closed to new replies.