Skip Navigation

Not possible to compare multiple custom field filters with OR instead of AND

Open

Symptoms

When adding more than one custom field filter to a View they are compared using the AND operator, and it is not possible within the UI to switch to compare with OR instead.

(There was a setting for this in the UI but it didn’t work correctly and has been removed until it is fixed.)

Workaround

You can use the Views API to modify the query arguments to specify OR as the relation operator, like so:

/**
 * Modify View custom field filters to use OR instead of AND
 */
function tssupp_filter_query_or($view_args, $view_settings, $view_id)
{
    $view_ids = array( 120 ); // Edit. Array of View IDs to modify

    if ( in_array($view_id, $view_ids) )
    {
        if ( !empty($query_args['meta_query']) )
        {
            $query_args['meta_query']['relation'] = 'OR';
        }
    }

    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query_or', 101, 3);

(Note, you should only need to edit the IDs of the Views where this is to be applied.)

3 thought on Not possible to compare multiple custom field filters with OR instead of AND

  • I trie adding this snippet under the Custom Code tab but I’m having this error message
    “ There was an error when updating snippets.”

    • Sounds like you somehow introduced a problem pasting in the code. If you open a support thread someone will be able to take a look at what you have done to see (include a screenshot, otherwise you may need to provide access to your site).

  • Why is this still not resolved properly? I flagged it in September last year and was told it would be fixed ‘soon’! It seems a fairly fundamental requirement for a query engine that you should be able to switch between AND and OR…

Leave
a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>