I have a working script responsible for displaying woocommerce products on my page which goes something like this:
<?php
$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(); ?>
<h4>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h4>
<?php the_excerpt(); ?>
<a class="listed-product-button" href="<?php the_permalink(); ?>">More</a>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<div>
<?php _e( 'No products' ); ?>
</div>
<?php endif; ?>
</div>
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?
I have a working script responsible for displaying woocommerce products on my page which goes something like this:
<?php
$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(); ?>
<h4>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h4>
<?php the_excerpt(); ?>
<a class="listed-product-button" href="<?php the_permalink(); ?>">More</a>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<div>
<?php _e( 'No products' ); ?>
</div>
<?php endif; ?>
</div>
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?
1 Answer
Reset to default 0If 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/Class_Reference/WP_Query#Category_Parameters http://codex.wordpress/Class_Reference/WP_Query#Taxonomy_Parameters