Skip Navigation

[Resolved] Using the AND function in a search with checkboxes

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

Problem:
Using the AND function in a search with checkboxes

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

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/using-the-and-function-in-a-search-with-checkboxes/#post-2335277

Relevant Documentation:
=> => https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 2 years, 9 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 eveD 2 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2335049

Tell us what you are trying to do?

I have a search with checkboxes.
Users select as many options as relevant, and I need to show results that match ALL selected options.
Right now, it is showing results that match ANY option.
(ie I need to connect with AND not OR)

Is this possible?

#2335059

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I would like to know what view you are using, are you using Block view or classic view.

Can you please share problem URL where you added your view with checkboxes filter and share 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.

#2335081

Minesh
Supporter

Languages: English (English )

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

Ok as you created a view using the legacy editor.

You should try to edit your view and navigate to the "Query Filter" section and for your checkboxes field query filter you should try to choose the comparison "in" instead of "equal to".

If you cannot see the Query Filter section, activate it in the Screen Options tab available at top right corner.

#2335109

I found the Query Filter, thanks.
But the IN operator acts like a "OR".

Example:
If I want an apartment with Wifi, Pool and parking and I check of all 3 options in my results I will get ALL apartments that have Wifi, ALL that have pools and ALL that parking, even if the apartments don't have all 3 options.

I need to return apartments that have Wifi AND parking AND pool.

#2335111

Minesh
Supporter

Languages: English (English )

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

If you do not use the "in" operator, by default, it should act as "And" clause when you select.

Do you have checkboxes filter setup as taxonomy or custom field?

#2335131

My checkbox is a custom field

#2335133

Minesh
Supporter

Languages: English (English )

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

Can you please share screenshot of your checkboxes custom field from: Toolset => Custom Fields => Edit your custom field group => take the screenshot of your checkboxes field and share with me.

#2335135
Screenshot 2022-04-06 at 12.12.14.png

Sure, please see attached

#2335159

Minesh
Supporter

Languages: English (English )

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

So, checkboxes fields are special fields whose value stored as serialized array to database.

Do you have any other checkboxes filters added to your view? As this is really specific thing, if you can share problem URL and access details that will help me to know what filters are added to view and what workaround I can offer you and what could be the best workaround.

*** 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.

#2335259

It's fine...if there is no obvious quick solution I can do a work around where each of the checkboxes will be a separate field , and then all those will be joined naturally by the AND operator.

Thanks!

#2335277

Minesh
Supporter

Languages: English (English )

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

If you do not have complicated view then the following filter should work:

Can you please try to add the following filter to the "Custom Code" section offered by Toolset, make sure you activated the snippet you add.
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpv_filter_query', 'func_change_checkboxes_clause_to_and',99,3 );
function func_change_checkboxes_clause_to_and( $query_args ,$view_settings,$view_id) {
if ($view_id==9999) {
   
      foreach((array)$query_args['meta_query'] as $k=>$v):  	
                 foreach($v as $x=>$y):
					if($x=='relation'){
						$query_args['meta_query'][$k]['relation'] = 'AND';
                      }
                  endforeach;
        endforeach;
    
}
return $query_args;
}

Where:
- Replace 9999 with your original view ID.

#2335307

Oh my goodness, that works brilliantly!

Thank you SO much.

#2335309

My issue is resolved now. Thank you!