WordPress.org

Ready to get started?Download WordPress

Forums

Does get_posts() support tax_query? (4 posts)

  1. helgatheviking
    Member
    Posted 3 years ago #

    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.

  2. Azuaron
    Member
    Posted 3 years ago #

    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.

  3. helgatheviking
    Member
    Posted 3 years ago #

    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.

  4. Azuaron
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic