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?
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
Is it possible to avail this feature to a particular view and keeping all other views to 'AND' Logic?
Yes, to limit that function to filter only specific view(s), you can change the conditional check at line # 4, from:
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.
My issue is resolved now. Thank you!