0

I'm currently working on shop page and custom product loop. With WP_Query I can succesfully display products based on meta_key and meta_val but i run into trouble. Now i have to display all products from category and as argument i need to pass category_id. This is my code:

$args = array(
            'post_type' => 'product',
            'posts_per_page' =>12,
    //   get category by slug is working -
            'product_cat' =>  'products-from-cat-40',
          );

I know that woocommerce have slightly different query arguments than wordpress so i've tried so far:

  • product_cat_id
  • product_cat
  • product_category
  • product_category_id
  • cat
  • cat_id
  • category_id

As ID i passed both integer and string but still nothing works. I've searched trough internet for list of viable Woocommerce WP_Query arguments and i couldn't find it. Can anyone help? I'll much appreciate.

  • And does the category with slug 'products-from-cat-40' really exist? – Krzysiek Dróżdż Apr 25 '18 at 6:27
  • That was only example. The problem is solved so i guess you want to use this code in your case? – Bartosz Was Apr 25 '18 at 16:30
0

For custom taxonomies, including Product Categories, you should use the tax_query argument:

$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 12,
    'tax_query'      => array(
        array(
            'taxonomy' => 'product_cat',
            'terms'    => 40,
        ),
    ),
);
  • 1
    You should also avoid using WP_Query() to retrieve WooCommerce products. See more info here: cfxdesign.com/… – cfx May 24 '18 at 20:44

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.