2

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:

$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.

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' ) )
        )
    ) 
  • 1. Omitting term field woudln't help. 2. Did you mean 'field' => 'id', ;-)? – Oleg Butuzov Nov 20 '12 at 12:31
  • Nope; get_terms() args array uses 'fields => ids'. – Chip Bennett Nov 20 '12 at 12:34
  • Where did I wrote fields? – Oleg Butuzov Nov 20 '12 at 12:35
  • Which code block are you referring to with respect to 'field'? The tax_query array uses 'field' (singular), but get_terms() args array uses 'fields' (plural). – Chip Bennett Nov 20 '12 at 12:51
  • Oh, I see what you mean now. Yes: the 'tax_query' needs to use 'field' => 'id', because in that case, 'field' corresponds to the term, not to the taxonmy. Refer to the Codex for 'tax_query': "field (string) - Select taxonomy term by ('id' or 'slug')" – Chip Bennett Nov 20 '12 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.