0

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.

2 Answers 2

4

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'
     )
)
1
  • 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. Oct 12, 2012 at 12:21
0

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!

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.