Home > Net >  WordPress tax_query "AND" operator doesnt work
WordPress tax_query "AND" operator doesnt work

Time:05-10

I'm trying to filter based on a tax_query filter in WordPress but it doesnt give the right data back.

  $args = array(
    'post_type'         => 'cpt_zorgverleners',
    'post_status'       => 'publish',
    'posts_per_page'    => 12,               
    'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'cat_typezorg',
                    'field'    => 'slug',
                    'terms'    => array('dietist','apotheker'),
                    'operator' => 'AND'
                )
        ),        
    'orderby'           => 'title'
);

It does work when I remove the "operator AND" but I need that filter. With the operator AND it works with all the child taxonomies but not with the parent taxonomies. And without it it works fine but it functions like an OR operator and I don't want that.

Whats wrong with this? why doesnt it work with the parent taxonomies?

CodePudding user response:

You use only one array in tex_query then remove "relation" from tex_query

'tax_query'                     => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'dietist','apotheker' ),
            'operator' => 'AND',
        ),
    ), 

CodePudding user response:

Got it.. This was my fix:

$args = array(
'post_type'         => 'cpt_zorgverleners',
'post_status'       => 'publish',
'posts_per_page'    => 12,               
'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'cat_typezorg',
                'field'    => 'slug',
                'terms'    => array('apotheker'),
            ),
            array(
                'taxonomy' => 'cat_typezorg',
                'field'    => 'slug',
                'terms'    => array('dietist'),
            )
    ),        
'orderby'           => 'title'
);

only problem is I cant mix AND/OR now.. not needed but how do i do that?

tax_query
   AND
     - custom tax term 1
     - custom tax term 2
   AND
     - other custom tax term 1
     OR
     - other custom tax term 2
  • Related