Skip Navigation

[Resolved] And Logic Between Checkboxes

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by fahimS-2 2 months, 1 week ago.

Assigned support staff: Waqar.

Author
Posts
#2539473

Tell us what you are trying to do?
I am implementing a search filter. I have a custom field type of checkbox. Now by default the logic between the checkboxes is OR. But I need And logic between the checkboxes. Kindly help me.
Is there any documentation that you are following?
No

Is there a similar example that we can see?
No

What is the link to your site?

#2539685

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To change the comparison logic from 'OR' to 'AND' between the checkboxes type field's filter, you can use a custom function attached to the 'wpv_filter_query' filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example:


add_filter( 'wpv_filter_query', 'filter_checkboxes_or_to_and', 1000 , 3 );
function filter_checkboxes_or_to_and( $query_args, $view_settings ) {

	if ( !is_admin() ) {
		if( !empty($query_args['meta_query']) ) {
			foreach( $query_args['meta_query'] as $key => $value){
				if($key != 'relation') {
					foreach ($value as $subkey => $subvalue) {
						if(is_array($subvalue)) {
							if($subkey != 'relation') {
								if ($subvalue['key'] == 'wpcf-property-checkboxes') {
									$query_args['meta_query'][$key]['relation'] = 'AND';
								}
							}
						}
					}
				}
			}
		}
	}

	return $query_args;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

This function will look for the custom field filter used for the field "wpcf-property-checkboxes" in all the views. And, if found, will change its operator to 'AND' from 'TO'.
( you'll change the 'property-checkboxes' with the slug of the field used on your website )

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2539703

Is it possible to avail this feature to a particular view and keeping all other views to 'AND' Logic?

#2539727

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes, to limit that function to filter only specific view(s), you can change the conditional check at line # 4, from:


if ( !is_admin() ) {

To:


// list of target view IDs
$target_views = array(123, 456, 789);
// if the current view is among the target views
if ( !is_admin() && ( in_array($view_settings['view_id'], $target_views) ) ) {

As a result, the function will only apply this change in the operator to a view, if its ID is available in the list/array '$target_views'.
(you'll change the IDs 123, 456, 789, etc with the actual IDs of your views that you'd like to target for this snippet)

Note: You can see the IDs of your website's views from WP Admin -> Toolset -> Views. And if you don't see this section to manage views, please go to WP Admin > Toolset > Settings > General and select the option "Show both the legacy and Blocks interface and let me choose which to use for each item I build" for the editing experience.

#2539729

My issue is resolved now. Thank you!