Have a widget that's currently filtering a set of categories, and need to make it support more than one set of categories.
Right now the code is something like this:
$query_args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $categories,
'operator' => 'AND',
),
),
);
So, basically, it's filtering products that have x, y and z categories. Now I need it to filter products that have x and y, OR x and z. I've sketched this:
$query_args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND'
),
);
if(is_array($tags)){
$query_args['tax_query']['relation'] = 'OR';
foreach($tags as $tagGroup){
array_push($query_args['tax_query'], array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => implode(',', $tagGroup),
'operator' => 'AND'
));
}
}else{
array_push($query_args['tax_query'], array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $categories,
'operator' => 'AND',
));
}
But it's still not filtering products as I need. What am I doing wrong?