WordPress.org

Support

Support » Alpha/Beta » [Resolved] Does get_posts() support tax_query? RC4

[Resolved] Does get_posts() support tax_query? RC4

  • i’m trying to do some custom queries for a 2nd loop and typically I use get_posts so it doesn’t mess with the main query. i am using 3.1 RC4 and was trying to implement some of the new tax_query support.

    the following, using query_posts() works as expected.

    $myquery['tax_query'] = array(
    	array(
    		'taxonomy' => 'genre',
    		'terms' => array('fantasy'),
    		'field' => 'slug',
    	),
    );
    $myposts = query_posts( $myquery );

    however, even though the codex says that get_posts supports all the same arguments as query_posts, get_posts() does not return any values with the same arguments.

    $myquery['tax_query'] = array(
    	array(
    		'taxonomy' => 'genre',
    		'terms' => array('fantasy'),
    		'field' => 'slug',
    	),
    );
    $myposts = get_posts( $myquery );

    get_posts() does however seem to work if the taxonomy is one of the default WP taxonomies… like a category.

    $myquery['tax_query'] = array(
    	array(
    		'taxonomy' => 'category',
    		'terms' => array('artists'),
    		'field' => 'slug',
    	),
    );
    $myposts = get_posts( $myquery );

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Andrew Nacin

    @nacin

    Lead Developer

    Confirmed. Investigating.

    Moderator Andrew Nacin

    @nacin

    Lead Developer

    Cancel that. Cannot reproduce. get_posts() supports custom taxonomies through tax_query without issue.

    Please disable all plugins.

    thanks andrew! all plugins are disabled.

    does work if the taxonomy in tax_query is a tag or a category, but i still can’t get anything going with my custom taxonomies.

    have also tried adding rewrite->slug conditions to the register_taxonomies, but according to the codex shouldn’t need it as the taxonomy slug defaults to the name it was registered as.

    Moderator Andrew Nacin

    @nacin

    Lead Developer

    What’s your query var? That’s what it uses.

    This was my taxonomy registration (called on init):
    register_taxonomy( 'fruits', array( 'post' ), array( 'label' => 'Fruits', 'show_ui' => true ) );

    the tax_query is the only parameter i am attempting to use at the moment, though i will eventually add the ‘numberposts’ parameter.

    $myquery['tax_query'] = array(
    	array(
    		'taxonomy' => 'portfolio-tags',
    		'terms' => array('bacon'),
    		'field' => 'slug',
    	),
    );
    
    $myposts = get_posts( $myquery );
    
    print_r($myposts);

    prints an empty array, but in the back-end i have 3 custom posts of type portfolio that have the portfolio-tag of “bacon”.

    here is my register taxonomy function:

    // Builds the a tag taxonomies
    
    function create_portfolio_taxonomies() {
    	register_taxonomy( 'portfolio-tags', 'portfolio', array( 'hierarchical' => false, 'label' => 'Portfolio Tags', 'query_var' => true, 'rewrite' => array( 'slug' => 'portfolio-tags' ), ) );
    	}
    add_action( 'init', 'create_portfolio_taxonomies', 0 );

    Moderator Andrew Nacin

    @nacin

    Lead Developer

    It’s not tax_query 🙂

    get_posts by default only queries for posts. You’ll need to pass in your post type.

    ok got it. since i am trying to pull all posts from a custom taxonomy of a custom post type I apparently also have to declare the post_type argument as well. wouldn’t it be better if it worked without? if a query for custom taxonomy was independent of post type?

    thank you for your help. your taxonomy for regular posts finally got things to click in my mind.

    yup!! thank you so much. topic resolved.

    Moderator Andrew Nacin

    @nacin

    Lead Developer

    Well, get_posts() is a very specific function that sets its own defaults, then creates a new WP_Query object. You’d be better off creating your own WP_Query object and avoid get_posts().

    thanks agin andrew. i didn’t have much success creating my own query object and i usually prefer get_posts when creating a second loop.

    i thought i could get around that by passing in the appropriate post_type parameter, but can’t get get_post_types to return the right post type based on the taxonomies parameter.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Does get_posts() support tax_query? RC4’ is closed to new replies.
Skip to toolbar