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 Answer 1

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' ) )
        )
    ) 
11
  • 1. Omitting term field woudln't help. 2. Did you mean 'field' => 'id', ;-)? Commented Nov 20, 2012 at 12:31
  • Nope; get_terms() args array uses 'fields => ids'. Commented Nov 20, 2012 at 12:34
  • Where did I wrote fields? Commented Nov 20, 2012 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). Commented Nov 20, 2012 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')" Commented Nov 20, 2012 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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