Skip Navigation

AND always used with multiple Views query filters, even if OR is specified

Open

Topic Tags: Views, Views plugin

Symptoms

When multiple search filters or query filters are added to a View, the legacy editor for Views allows you to specify whether the condition for matching multiple filters should use AND or OR. (The option isn’t currently available in the block editor.)

Selecting OR doesn’t work, the generated query always applies AND as the condition for multiple filters.

Workaround

It would be possible to modify the query generated by Views to force the OR comparison using the API filter wpv_filter_query, with a suitably high priority (e.g. 101). See https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

An example of the kind of code that can be used to switch the default AND comparison to OR would be:

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
    if (in_array($view_id, array( 117 ))) { // Edit View ID(s)

        if ( isset( $view_args['meta_query']['relation'] ) )
        {
            $view_args['meta_query']['relation'] = 'OR';
        }
    }

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

3 thought on AND always used with multiple Views query filters, even if OR is specified

  • Why is this still an issue? Views is a query engine that only supports AND queries? Come on folks…

  • This would be helpful. Sadly, the workaround above doesn’t work for all query filters.
    e.g. Filter by post relationship or repeatable fields group owner OR Post author filter

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>