Hi there.
I have the following problem
I have a custom post type that have a several custom fields. One of them are checkboxes.
The custom search to show that custom post types is configured AJAX results update when visitors change any filter values.
When I click in one checkbox works fine and the results are ok. If I click 2 checkboxes that works wrong because the results are working with relation OR not AND.
How can I solve this?
Example:
Restaurants with wifi: 2 restaurants
Restaurants with terrace: 1 restaurant
Restaurants with wifi + terrace = 1 restaurant (AND) but custom search show 3 restaurants (OR).
How can i solve this?
Thankss in advanced
Nigel
Supporter
Idiomas:
Inglés (English )
Español (Español )
Zona horaria:
Europe/London (GMT+00:00)
I guess you are doing this in Blocks?
When I look at doing this in classic Views I can see an option for this when inserting the filter (screenshot).
But the same options are missing from the View block.
I'm adding this to the development board for classic features which have not been fully migrated to blocks.
In the meantime, you could switch to creating the View in classic Views.
Alternatively, you could add a code snippet that uses the wpv_filter_query API hook and changes the comparison argument to AND.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Hi Nigel and thanks for your answer.
I am working with views (old way) and I don't see this dropdown that you show in that screenshot.
Look this pictures...
Is there any way to enable that comparison dropdown?
Nigel
Supporter
Idiomas:
Inglés (English )
Español (Español )
Zona horaria:
Europe/London (GMT+00:00)
Sorry, my bad.
My screenshot comes from a taxonomy filter, not a custom field filter.
I think we'll need to go down the route of a code snippet to modify the query.
Let me check what's required on my own test site and I'll get back to you, probably in the morning.
Hi Nigel.
Wait please. I found this: https://toolset.com/forums/topic/how-to-filter-by-more-than-one-checkbox/#post-631805
I'll check it.
If I need your help I'll post here again.
Thanks again!
Nigel
Supporter
Idiomas:
Inglés (English )
Español (Español )
Zona horaria:
Europe/London (GMT+00:00)
I had a quick look at that link, I wasn't sure about the solution, so you can try this if you didn't get that to work:
/**
* Modify View query filters for checkboxes custom field filter
* to compare with AND instead of default OR
*/
function tssupp_mod_checkboxes_query($view_args, $view_settings, $view_id)
{
$slug = 'colours'; // Edit slug
$metakey = 'wpcf-'.$slug;
if (in_array($view_id, array( 88 ))) { // Edit View ID(s)
$meta_query_keys = array_keys($view_args['meta_query']);
unset($meta_query_keys['relation']);
foreach ($meta_query_keys as $meta_query_key){
if ( is_array( $view_args['meta_query'][$meta_query_key] ) && isset($view_args['meta_query'][$meta_query_key][0]['key']) && $view_args['meta_query'][$meta_query_key][0]['key'] == $metakey ){
$view_args['meta_query'][$meta_query_key]['relation'] = 'AND';
}
}
}
return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_mod_checkboxes_query', 101, 3);
You just need to edit the slug of your checkboxes field and the ID of the View.
Thanks for your help Nigel.
My issue is resolved now. Thank you!