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( 'taxonomy' => 'product_cat', 'terms' => 17 ), ); $products = new WP_Query($args);
And this also returns every 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
tax_queryreturns every products, ignoring my
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 ?
EDIT : the code snipet with
tax_query was wrong so I changed it.
EDIT 2 : I have tried several new Things, here is the summary :
- disabled all custom hooks : same results
- instantiated a
WC_Productmanually by the id of an actual product as an argument. It shows that its
category_idsattribute is empty, even though the product does have a category on the admin panel... and the category taxonomy page shows the right stuff too.
- when I do
var_dump(get_the_terms($postID, 'category'));on a regular post it works fine
EDIT 3 :
- disabled all plugins but Woocommerce with same result...
- when I do
var_dump(get_post_types());, the product post type does not show. And so naturally, when I do
var_dump(get_object_taxonomies('product'));, it returns an empty array.