Skip Navigation

[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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 4 replies, has 2 voices.

Last updated by Mateus Getulio 5 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2708805

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.

Thank you!

#2708847

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi,

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

To suggest the best way to achieve this, I'll need to see how this view is set up in the admin area.

Can you please share temporary admin login details along with a link to a page where this view can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Mateus

#2709216

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello Lucy,

Thank you for sharing the access to the site.

I'm having a hard time troubleshooting this.

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.

The other workaround would be for us to change the operator to 'AND' using custom code. We can use wpv_filter_query to perform this action.

It is close to what was done here.

I tried adapting the code in the example above to work in your set up, but for some reason the operator is not changing.

I wanted to check with you if it is OK for me to switch to a default theme and disable some plugins to check if there might be a conflict issue.

Based on the URL, it looks like it is a staging site, but I wanted to double-check first.

Thank you,
Mateus

#2709217

Hi Mateus

Thank you for your help. Yes, it is a staging site and I have just taken a backup so please go ahead and disable plugins, etc, as you need to.

Thanks
Lucy

#2709253

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello Lucy,

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?

Thank you,
Mateus

#2709408

Thank you very much for your help. That is working correctly now.