Get the product list of a given Category ID

Question

I couldn’t find the right way to get the list of all products for a given category ID (not category name).

The code I am using to get the categories list is the following, it works fine:

$args = array(
           'orderby'    => $orderby,
           'order'      => $order,
           'hide_empty' => 0,
           'include'    => $ids,
           'parent'    => 0,
     ); 

$categories = get_terms( 'product_cat', $args );

However, now for a given category ID (say 47), I couldn’t find the way to get its relevant products. I tried the following way:

$args = array( 
    'posts_per_page' => 5,
    'offset'=> 1,
    'category' => 47
 );

$products = get_posts( $args );
echo var_dump($products);

Debugging the $products array returns always 0 which is wrong since I know there is some products under the category with ID 47. Any ideas how to fix my code?

0
Malloc 3 years 2020-06-08T04:10:33-05:00 0 Answers 84 views 0

Leave an answer

Browse
Browse