wp query - WP_Query Taxonomy categories filtering

Have a widget that's currently filtering a set of categories, and need to make it support more than one set of cate

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?

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?

Share Improve this question asked Jul 10, 2019 at 13:15 PabloPablo 9110 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can nest your tax queries. Just make sure you use OR as the first relation and AND relation on the nested tax queries.

$filter_group_a = array(
  'relation' => 'AND',
  array(
    'taxonomy' => 'product_cat',
    'field'    => 'slug',
    'terms'    => $term_a,
  ),
  array(
    'taxonomy' => 'product_cat',
    'field'    => 'slug',
    'terms'    => $term_b,
  ),
);

$filter_group_b = array(
  'relation' => 'AND',
  array(
    'taxonomy' => 'product_cat',
    'field'    => 'slug',
    'terms'    => $term_a,
  ),
  array(
    'taxonomy' => 'product_cat',
    'field'    => 'slug',
    'terms'    => $term_c,
  ),
)

$query_args = array(
  'tax_query' => array(
    'relation' => 'OR',
    $filter_group_a,
    $filter_group_b,
  )
);

You could write some fancy loop that builds the $filter_groups and pushes them to the $query_args['tax_query'] array.

you can nest multiple tax queries like so:

$query_args = array(
        'post_type' => 'product',
        'tax_query' => [
            'relation' => 'OR',
            [
                [
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => $x_y,
                    'operator' => 'AND',
                ],
            ],
            [
                'relation' => 'AND',
                [
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => $x_z,
                    'operator' => 'AND',
                ],
                [
                    'taxonomy' => 'something_something',
                    'field' => 'slug',
                    'terms' => $z,
                    'operator' => 'AND',
                ]
            ],

        ],
    );

i took your example code, so not sure about all the values you have there, but basically you can combine AND and OR.. more info here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330524a4622856.html

相关推荐

  • wp query - WP_Query Taxonomy categories filtering

    Have a widget that's currently filtering a set of categories, and need to make it support more than one set of cate

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信