简体   繁体   中英

Get all products which have both product category

I need all products which have both categories. I am using a query below:

$args = array(
                            'post_type' => array('product', 'product_variation'),
                            'paged' => $paged,
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'product_cat',
                                    'field' => 'slug',
                                    'terms'    => array( 'shop', 'cat1' ),
                                    'operator' => 'AND',
                                )
                            )
                        );

It's working correctly if cat1 category does not have any subcategory like cat1-1,cat1-2 etc. But when i made subcategory in backend of cat1 than result will give zero.

Query is same just no result if cat1 have subcategory.

Thanks

Add include children in your arguments.

Make sure your products have both categories.

In case you want to filter by multiple categories operator could be 'IN' instead of 'AND'.

$args = array(
'post_type' => array('product', 'product_variation'),
'paged' => $paged,
'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'include_children' => true,
            'field' => 'slug',
            'terms'    => array( 'shop', 'cat1' ),
            'operator' => 'AND',
        )
    )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM