Skip Navigation

[Resolved] Filter with AND instead of OR

This thread is resolved. Here is a description of the problem and solution.

Problem:
Filter with AND instead of OR - how to filter view results with And with checkboxes options instead of OR clause

Solution:
You will have to use view's filter hook "wpv_filter_query" to modify the view's query on fly.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/filter-with-and-instead-of-or/#post-2042259

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 12 replies, has 2 voices.

Last updated by Marcel 2 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#2040201

I need the last filter on hidden link to filter as AND, so ALL checked values should be true for a result to turn up.

At the moment, it's set as OR, so if a listing has ANY of the checked values, it will show up. That's not what I need.

I can't find a setting for that, so how do I change it?

#2040449

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand, for the filter "Bezetting", for instance if user select the checkboxes option "Allround dj" and "Zanger" you want to only display the results where both "Allround dj" and "Zanger" are checked - correct? if yes:
- You will require to use the view's filter wpv_filter_query to adjust the view's query on fly.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you do not know how to do it, please send me admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2040935

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I just noticed that you are using the archive instead of view.

There is no way to change the clause to "And" instead of "Or" for the custom filter when using the archive. I checked both pre_get_posts and posts_where hook but I do not see any easy way to change the query as required.

#2041033

Hi Minesh,

Okay, I recreated it as a view on hidden link. Can you get it to work there?

#2041189

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've added the following code to the "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query', 'func_change_filter_clause',10,3 );
function func_change_filter_clause( $query_args ,$view_settings,$view_id) {
if ($view_id == 1228 ) {
  
  
    foreach((array)$query_args['meta_query'] as $k=>$v):
            if(isset($v['key']) and ($v['key']=='wpcf-bezetting')){
              
                $values = explode(",",$v['value'][count($v['value'])-1]);
                if(count($values) > 1){
                    $key = $v['key'];
                    unset($query_args['meta_query'][$k]);
                     
                    foreach($values as $x=>$y):
                        $query_args['meta_query'][$k][$x]['key'] = $key;
                        $query_args['meta_query'][$k][$x]['type'] = 'CHAR';
                        $query_args['meta_query'][$k][$x]['compare'] = 'LIKE';
                        $query_args['meta_query'][$k][$x]['value'] = $y;
                    endforeach;
                    $query_args['meta_query'][$k]['relation'] = 'AND';
                }
            }
        endforeach;
   
}
return $query_args;
}

can you please confirm it works as expected.

#2041399

Hi Minesh,

Thanks, that seems to work 🙂

Now I just need the view to show only results from the current category. I created a template for artist categories in Divi's theme builder with this view, but the view shows all results instead of only the results of the current category. Can you help me with that as well?

And I'm doubting to filter the "muziekgenre" the same way. How can I add that to your code?

#2041501

Never mind the first question, figured that out already. I was still using the category filter in my user filters, when I removed it there, I could configure the view to only show posts in the current category.

Then only my second question remains: "And I'm doubting to filter the "muziekgenre" the same way. How can I add that to your code?"

#2041641

Never mind, figured the second question out as wel by Googling how to add multiple conditions in a PHP statement (via ||).

Thanks for your extensive support as always 🙂

#2041689

Hmm there still seems to be one problem... auto refresh doesn't seem to work correctly on this page: hidden link. I have to manually refresh the page to apply the filters.

That's a copy of the view used on the category pages, which has a different ID. I modified your code to add that ID, but I probably did something wrong. I changed the code to

add_filter( 'wpv_filter_query', 'func_change_filter_clause',10,3 );
function func_change_filter_clause( $query_args ,$view_settings,$view_id) {
if ($view_id == 1228||1331 ) {

Where 1331 is the second ID.

#2042259

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now. I've adjusted the code added to "Custom Code" section as given under:

add_filter( 'wpv_filter_query', 'func_change_filter_clause',10,3 );
function func_change_filter_clause( $query_args ,$view_settings,$view_id) {
if ($view_id==1228 || $view_id==1331) {
  
  
    foreach((array)$query_args['meta_query'] as $k=>$v):
            if(isset($v['key']) and ($v['key']=='wpcf-bezetting' || $v['key']=='wpcf-muziekgenre')){
              
                $values = explode(",",$v['value'][count($v['value'])-1]);
                if(count($values) > 1){
                    $key = $v['key'];
                    unset($query_args['meta_query'][$k]);
                     
                    foreach($values as $x=>$y):
                        $query_args['meta_query'][$k][$x]['key'] = $key;
                        $query_args['meta_query'][$k][$x]['type'] = 'CHAR';
                        $query_args['meta_query'][$k][$x]['compare'] = 'LIKE';
                        $query_args['meta_query'][$k][$x]['value'] = $y;
                    endforeach;
                    $query_args['meta_query'][$k]['relation'] = 'AND';
                }
            }
        endforeach;
   
}
return $query_args;
}

Can you please confirm it works as expected.

#2042289

Hi Minesh,

Sorry, still doesn't work. See this screenrecording: hidden link.

#2042321

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure whats going on as even I disabled the code I added and when I try to filter it, I see the same result as you can.

It seems there is a cache issue. Can you please disable the cache you are using and then try to activate the hook that I commented out and check.

#2042351

Hi Minesh,

I found the problem. I embedded another view in the loop item to show values from a related post type. That caused the filter system not to work correctly. I now removed that embedded view, now it works again. So it had nothing to do with your code indeed 🙂

Thanks again,

Best regards
Marcel

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.