Query all posts in a given taxonomy

For sure it’s very simple, but I can not figure out how to do this.

I need to query all posts that has a specific taxonomy set. Like this:

Read More
$query03 = array( 
'numberposts' => 5, 
'post_type' => array(
    'video'
    ), 
'tax_query' => array(
        array(
            'taxonomy' => 'product',
            'field' => 'slug',
            'terms' => **all_the_terms_in_taxonomy**
        )
    ) 
);

I want set the terms field a wildcard. I know I could populate terms fild with an array, but the list is very very long and I’m worried about weight of the query.

Related posts

Leave a Reply

1 comment

  1. Have you tried simply omitting the 'terms' key from the 'tax_query' array?

    $query03 = array( 
        'numberposts' => 5, 
        'post_type' => array(
            'video'
        ), 
        'tax_query' => array(
            array(
                'taxonomy' => 'product',
                'field' => 'slug'
            )
        ) 
    );
    

    Alternately, I wouldn’t really worry about the weight of the query. The resulting query will be what it will be, regardless of how complex the query args, or how big the query args array. So, I would recommend just pulling in all of your terms via get_terms:

    <?php get_terms( $taxonomy, $args ); ?>
    

    You can use it directly in your query args array, by setting 'fields' to 'ids', which will return an array of term IDs, rather than an array of term objects):

    $query03 = array( 
        'numberposts' => 5, 
        'post_type' => array(
            'video'
        ), 
        'tax_query' => array(
            array(
                'taxonomy' => 'product',
                'field' => 'id',
                'terms' => get_terms( 'product', array( 'fields' => 'ids' ) )
            )
        ) 
    );
    

    Edit

    Alternate ‘tax_query‘, using term slugs instead of IDs:

        'tax_query' => array(
            array(
                'taxonomy' => 'product',
                'field' => 'slug',
                'terms' => get_terms( 'product', array( 'fields' => 'names' ) )
            )
        )