I have a working script responsible for displaying woocommerce products on my page which goes something like this:

$params = array('posts_per_page' => 99, 'post_type' => 'product');
$wc_query = new WP_Query($params);
<div class="list-of-products">
     <?php if ($wc_query->have_posts()) : ?>
     <?php while ($wc_query->have_posts()) :
                $wc_query->the_post(); ?>
     <div class="listed-product">
          <?php the_post_thumbnail(); ?>
               <a href="<?php the_permalink(); ?>">
               <?php the_title(); ?>
          <?php the_excerpt(); ?>
          <a class="listed-product-button" href="<?php the_permalink(); ?>">More</a>
     <?php endwhile; ?>
     <?php wp_reset_postdata(); ?>
     <?php else:  ?>
          <?php _e( 'No products' ); ?>
     <?php endif; ?>

It works fine, but now I wanted to use the same script on another page to only show products from certain category, so I changed the $params line to something like this

$params = array('posts_per_page' => 99, 'post_type' => 'product', 'category_name' => 'category-name');

Where in place of category-name is actuall name of the category I want to display. The problem is it doesn't display any products, instead showing the No products text I set up in case of having 0 products. I tried the same with 'cat' => 43 which is suppoused to be the category id but again no results. Is there something obvious I am missing here?


If you're actually using categories (and not a custom taxonomy) then your category-name needs to be the slug, not the actual name.

If, however, you are using a custom taxononomy (and you'll know if you are) then you should use

 'name-of-your-custom-tax' => 'slug'     

You can also see the below links for details. http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

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.