Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type called 'product' with a registered taxonomy 'product-type'.

I want to display all post from a particular taxonomy from custom post type. In my case I want to display all the posts from 'product' post type with a term of 'software'.

    <?php $args = array( 
        'post_type' => 'product', 
        'tax_query' => array(
            'taxonomy' => 'product-type',
            'terms' => array('software'),
            'field' => 'slug'
        ),
        'posts_per_page' => 9, 
        'orderby' => 'menu_order', 
        'order' => 'ASC' 
    );

    $query = new WP_Query( $args ); ?>

    <ul class="list-products clearfix">

    <?php while ( $query->have_posts() ) : $query->the_post(); ?>

This is producing me no results. It's empty.

But if I remove the entire tax_query from $arg

'tax_query' => array(
    'taxonomy' => 'product-type',
'terms' => array('software'),
'field' => 'slug'
),

Everything works fine. I get all the posts from 'product' custom post type.

share|improve this question
add comment

2 Answers 2

If you look at the documentation for WP_Query taxonomy parameters, you'll see that the tax_query is an array of arrays.

So:

'tax_query' => array(
    array(
        'taxonomy' => 'product-type',
        'terms' => array('software'),
        'field' => 'slug'
     )
)
share|improve this answer
    
Hey guys.. I found the problem. I wasn't adding array in tax_query. I forgot it is an array of array. Thanks you all for looking into it. –  Sahil Mepani Oct 12 '12 at 12:21
add comment

tax_query is an array of array, also id you use another array to specify your terms you should also specity an operator. So your $args should be something like this:

 $args = array( 
      'post_type' => 'product', 
      'tax_query' => array(
         array(
           'taxonomy' => 'product-type',
           'terms' => array('software'),
           'field' => 'slug',
           'operator' => 'IN'
         )
       ),
      'posts_per_page' => 9, 
      'orderby' => 'menu_order', 
      'order' => 'ASC'
    );

Hope it helps!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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