I have a simple following logic:

$args = array(
    'post_type' => array('product'),
    'product_cat' => 280,
    'posts_per_page' => 3       

$category_posts = new WP_Query( $args );

    $result = '<div class="three-products">';



        $result .= '<div class="one-product">'.the_post_thumbnail( 'thumbnail' ).'</div>';
        $result .= '</div>';


        return $result;

     _e( 'There were no products matching your criteria.' );

I am sure that there are products in category 280, however have_posts always returns false.

If I comment that line out, it seems to end in infinite loop and does not load page at all.

Any help or guidance is much appreciated.

  • Use a proper tax_query, your taxonomy syntax is incorrect, that is where your code is failing Nov 6 '15 at 8:23
  • Could you please elaborate on that? I have found some examples of tax_query, however I cannot seem to get it correct.
    – WpDoe
    Nov 6 '15 at 8:27

Your arguments should look like this :

$args = array(
  'post_type' => 'product', // No need for an array since you only query one post type
  'posts_per_page' => 3,
  'tax_query'=>array(      // The taxonomy query
        'taxonomy'  => 'product_cat',
        'field'     => 'term_id', // Can be omitted, default parameter, can also be 'slug' or 'name' (name should be avoided as @Pieter Goosen says)
        'terms'     => 280       // You can use an array to include multiples terms i.e. array(280,281)

It doesn't work because wordpress only handle native taxonomy (category), for custom taxonomies, you must use a tax_query.

  • You can omit the field parameter here as its default is term_id ;-) Nov 6 '15 at 8:44
  • You are right ! Thought it would be more clear this way, going to do a little edit
    – Sladix
    Nov 6 '15 at 8:45
  • Great. Just one note on the field parameter, you should avoid using the name value. Due to very bad sanitation in the WP_Tax_Query class, the term is not recognised and therefor your query fails. The last ticket I saw regarding this issue, it will not be fixed. Nov 6 '15 at 8:52

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.