[Resolved] Use AND when filtering by custom taxonomy
This thread is resolved. Here is a description of the problem and solution.
Problem:
The customer needed to filter posts in her directory by a custom taxonomy called Features, ensuring that only posts with all selected tags were displayed (using an AND operator instead of OR).
Solution:
We determined that Toolset Blocks does not natively support changing the operator from OR to AND for multiple select fields. As a workaround, we enabled legacy views and configured the custom search to use the AND operator. Additionally, we used custom code with the wpv_filter_query filter to change the operator to AND. The specific code was added to the theme's functions.php file, targeting the correct view_id:
add_filter('wpv_filter_query', 'custom_taxonomy_query_operator', 10, 3);
function custom_taxonomy_query_operator($query_args, $view_settings, $view_id) {
if ($view_id == 509) {
if (isset($query_args['tax_query'])) {
foreach ($query_args['tax_query'] as &$tax_query) {
if (isset($tax_query['operator']) && $tax_query['operator'] == 'IN') {
$tax_query['operator'] = 'AND';
}
}
}
}
return $query_args;
}
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.
I am creating a search for my directory on this page: hidden link
I have a custom taxonomy called Features.
When I filter by Features I only want to find posts that have ALL of the selected tags.
E.g when I filter by 'Accommodation available' I get 3 results. When I filter by 'Accommodation available' and 'Onsite parking available' I only want to see 2 results because 'Celesta Venues' does not have parking.
So when filtering by multiple Features, I want to use AND not OR for the features checked.
In summary, Toolset Blocks doesn't have the option for you to change the operator from OR to AND when it is a multiple select field such as the checkboxes for example. This feature has been requested, but we don't have an ETA for its implementation.
One workaround is to enable the legacy views and to configure the custom search in there, using the legacy Views you're able to select within the UI the type of operator you want. Similarly to what my colleague described here.
I made a few more tests and came across the following code to change the operator of the multiple checkboxes search to 'AND' instead of 'OR':
add_filter('wpv_filter_query', 'custom_taxonomy_query_operator', 10, 3);
function custom_taxonomy_query_operator($query_args, $view_settings, $view_id) {
if ($view_id == 509) {
if (isset($query_args['tax_query'])) {
foreach ($query_args['tax_query'] as &$tax_query) {
if (isset($tax_query['operator']) && $tax_query['operator'] == 'IN') {
$tax_query['operator'] = 'AND';
}
}
}
}
return $query_args;
}
I added this code to your theme's functions.php file.
It is important to use the exact view_id which in the staging site is 509.
After adding the code, I selected the features 'Accommodation available' and 'Onsite parking available' in the search and only two results where shown.
Can you please test it and confirm that it is working now?