I'm having some trouble figuring out how to have checkboxes in a Views search filters work with AND, rather than OR., so the results should narrow down rather than expand as more selection are made. In my search filters I have "Property Type" checkboxes and in the Query Filter I've tried "equal to" and in, but both seem to cause AND results. Does it perhaps have anything to do with how the custom field is set up? I'm using "Show one of these two values:" Maybe this only works correctly if "Display the value of this field from the database" is selected?
Also, in the Sleeps select field the numbers don't arrange sequentially.
hidden link
Tim
Sorry, ignore the 2nd question, I found order="ascnum" solves that.
Hi,
Thank you for contacting us and I'd be happy to assist.
If you'd like to search through the "Property Type" so that only the results which contain all the selected types are shown, you'll need to add the "Property Types" as a custom taxonomy, instead of a custom field.
In a taxonomy search, it is possible to set the search type to either "All" (AND) or "Any" (OR), but in a custom field search, it uses only the "Any" (OR) operation.
You'll find the setting for the search type in the view's "Query Filter" section. In the taxonomy filter, you'll see that you'll have the option to select the search type to "any of the values" (OR), "not one of the values" (EXCLUDE), or "all of the values" (AND).
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
And I just tried the taxonomy approach, but instead of providing the options in the Query Filter that you mentioned it says "Filter based on the frontend search filter by Listing Types." with no options.
Thanks for writing back.
The other support thread is very old and a number of changes have been introduced to how the query filter is processed.
On my test website, I was able to change the filtering behavior for a checkboxes type field, using the "wpv_filter_query" ( from "OR" to "AND" ):
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
// skip if blocks edit screen
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return $query_args;
}
// process if specific view
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
$target_field_slug = 'checkboxes-field-slug';
if( !empty($query_args['meta_query']) ) {
for ($i=1; $i < (count($query_args['meta_query'])) ; $i++) {
for ($x=1; $x < (count($query_args['meta_query'][$i])-1) ; $x++) {
if(isset($query_args['meta_query'][$i][$x]['key']) && (!empty($query_args['meta_query'][$i][$x]['key']))) {
if ( $query_args['meta_query'][$i][$x]['key'] == 'wpcf-'.$target_field_slug ) {
$query_args['meta_query'][$i]['relation'] = 'AND';
}
}
}
}
}
}
return $query_args;
}
Note: Please replace "12345" with your actual view's ID and "checkboxes-field-slug" with your actual 'checkboxes' type field's slug.
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.
Your observation is correct and currently, if the query filter for a taxonomy or custom field is added automatically by adding a custom search field, it is not possible to edit that query filter's options.
We have an internal ticket to improve this in future releases, but, I don't have a time estimate to share at the moment.
For now, a workaround is to:
1. copy the search field's code from the "Search and Pagination" section and then remove it
2. when you see the message to remove its query filter, select 'yes' so it is removed too.
3. next add the query filter for that taxonomy/custom field manually and then paste back the search field's code in the "Search and Pagination" section.
Awesome this works!
If I want to add a second checkboxes field to that code, how would I do that? I tried separating by a comma like this but it did not work:
$target_field_slug = 'listing-amenities, type-of-property';
And thanks for the workaround for the other issue.
Tim
Thanks for the update and glad that it worked.
To make the code work for multiple checkboxes type fields, you'll need to update it slightly:
add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
// skip if blocks edit screen
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return $query_args;
}
// process if specific view
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
$target_field_slugs = array('wpcf-checkboxes-field-slug-1', 'wpcf-checkboxes-field-slug-2');
if( !empty($query_args['meta_query']) ) {
foreach ($query_args['meta_query'] as $key => $value) {
if($key != 'relation') {
if(in_array($value[0]['key'], $target_field_slugs)) {
$query_args['meta_query'][$key]['relation'] = 'AND';
}
}
}
}
}
return $query_args;
}
Please note how multiple target fields slugs have been passed through the array $target_field_slugs and make sure to include "wpcf-" prefix with them.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
My issue is resolved now. Thank you!