Get product list of given category

I want to get a list of products of a given category from WP_query, but it won’t work like it should.

I’ve done this :

$args = array(
‘post_type’ => ‘product’,
‘product_cat’ => 17,
);
$products = new WP_Query($args);

But this returns every product from my shop … I have also tried with ‘cat’, ‘category’ and ‘category_name’ attr with same result.

I’ve tried using tax_query :

$args = array(
‘post_type’ => ‘product’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘product_cat’,
‘terms’ => 17
),
),
);
$products = new WP_Query($args);

And this returns ZERO products
I have also tried with ‘cat’, ‘category’ and ‘category_name’ with same result.

I have managed using the following code to get regular posts from a given category.

$args = array(
‘post_type’ => ‘post’,
‘cat’ => 22
);
$posts = new WP_Query($args);

A couple more things :

I am certain I have the right category id.

tax_query worked for the posts too

I have been looking to do this for days and tried every possible solution of similar questions on stack and other sites without success… Why doesn’t it work for products ?

Read more here:: Get product list of given category

Leave a Reply

Your email address will not be published. Required fields are marked *