简体   繁体   English

获取所有具有两个产品类别的产品

[英]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. 如果cat1类别不包含cat1-1,cat1-2等任何子类别,则它可以正常工作。但是当我在cat1的后端创建子类别时,结果将为零。

Query is same just no result if cat1 have subcategory. 如果cat1具有子类别,则查询相同,只是没有结果。

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'. 如果要按多个类别进行过滤,则运算符可以为“ IN”,而不是“ 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',
        )
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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