WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Does get_posts() support tax_query? RC4 (11 posts)

  1. helgatheviking
    Member
    Posted 3 years ago #

    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 );
  2. Andrew Nacin
    Lead Developer
    Posted 3 years ago #

    Confirmed. Investigating.

  3. Andrew Nacin
    Lead Developer
    Posted 3 years ago #

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

    Please disable all plugins.

  4. helgatheviking
    Member
    Posted 3 years ago #

    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.

  5. Andrew Nacin
    Lead Developer
    Posted 3 years ago #

    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 ) );

  6. helgatheviking
    Member
    Posted 3 years ago #

    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 );
  7. Andrew Nacin
    Lead Developer
    Posted 3 years ago #

    It's not tax_query :-)

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

  8. helgatheviking
    Member
    Posted 3 years ago #

    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.

  9. helgatheviking
    Member
    Posted 3 years ago #

    yup!! thank you so much. topic resolved.

  10. Andrew Nacin
    Lead Developer
    Posted 3 years ago #

    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().

  11. helgatheviking
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic