1

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

$args = array(
                        'post_type' => array('product', 'product_variation'),

                        '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

1 Answer 1

1

Set include_children to false in the tax_query.

$args = array(
'post_type' => array( 'product', 'product_variation' ),

'tax_query' => array(
    array(
        'taxonomy'         => 'product_cat',
        'field'            => 'slug',
        'terms'            => array( 'shop', 'cat1' ),
        'include_children' => false,
        'operator'         => 'AND',
    )
)

);

See https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.