Skip Navigation

[Waiting for user feedback] Using OR instead of AND in Views filter queries

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.

This topic contains 1 reply, has 1 voice.

Last updated by Christopher Amirian 10 hours, 25 minutes ago.

Assisted by: Christopher Amirian.

Author
Posts
#2820837

Hello Toolset Support Team,

I have a View that lists therapists. Each therapist can have several custom fields for certifications ("Nachweise"). Each certification has a year field (for example: nachweis-basiskurs-jahr, nachweis-therapiekurs-1-jahr, etc.).

The View should only show therapists if all their certifications are still valid, meaning the year of the certification is greater than 2020. If any of the year fields is 2020 or lower, the therapist should be excluded from the results.

When I add multiple filters in the View for each custom field, they are combined with AND, which means the query only includes therapists that have all fields greater than 2020.
But in my case I need the opposite logic: as soon as one field is less than or equal to 2020, the therapist should be excluded. In other words, I need an OR combination of filters instead of AND.

I have checked the Toolset documentation on Views filters, conditions, and the wpv_filter_query hook. So far I only see options with AND logic, not OR.

Here is my View:
hidden link

Could you please let me know if there is a built-in way in Views to combine filters with OR logic, or if I need to use the wpv_filter_query hook to customize the query? Ideally, I would like to exclude therapists automatically if any of their certification year fields is less than or equal to 2020.

Thank you very much for your help!

#2820861

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. The Views GUI cannot combine multiple custom-field filters with OR. To get "exclude a therapist as soon as any certification year is 2020 or lower," use the wpv_filter_query hook to build a meta_query that mixes AND and OR.

A starter code that you can use:

add_filter( 'wpv_filter_query', function( $query_args, $view_settings, $view_id ){
    if ( (int) $view_id !== 12345 ) {
        return $query_args;
    }

    $cutoff  = 2020;
    // Put your Toolset field slugs here (as stored in DB).
    $fields = array(
        'wpcf-nachweis-basiskurs-jahr',
        'wpcf-nachweis-therapiekurs-1-jahr',
        'wpcf-nachweis-therapiekurs-2-jahr',
        // ...add more if needed
    );

    // Start or extend the meta_query
    if ( empty( $query_args['meta_query'] ) ) {
        $query_args['meta_query'] = array( 'relation' => 'AND' );
    } else {
        $query_args['meta_query']['relation'] = 'AND';
    }

    foreach ( $fields as $key ) {
        // For each field: allow if NOT SET OR > 2020.
        $query_args['meta_query'][] = array(
            'relation' => 'OR',
            array( 'key' => $key, 'compare' => 'NOT EXISTS' ),
            array( 'key' => $key, 'value' => $cutoff, 'type' => 'NUMERIC', 'compare' => '>' ),
        );
    }

    return $query_args;
}, 120, 3 );

Replace 12345 with your View ID and adjust the field slugs. This version keeps therapists when a given cert field is either missing or has a year > 2020; if a field exists and is ≤ 2020, the therapist is excluded.

You can add your custom code to Toolset > Settings > Custom code:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code

For more information about the filter_query:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Thanks.