When specifying Query Filters on multiple custom fields in the legacy interface, it used to be possible to specify that the filters should be logically ANDed or ORed.
This is illustrated in the screenshot in Christian Cox's first response in this forum topic (I couldn't find it in the legacy documentation):
https://toolset.com/forums/topic/views-query-filter-using-any-one-of-two-filters/
This AND / OR option seems to have disappeared.
I have a query filter which was created some years ago, with two filters ORed together:
Select items with field:
Event start date is a string greater than or equal TODAY()
OR
Event end date is a string greater than or equal TODAY()
(see: or-query1.jpg)
It still works, but if I Edit the filter, the AND / OR select list is not there.
(see: or-query2.jpg)
If I create a new View with a similar multi-field filter, the filters are automatically ANDed -- again, the AND / OR select list is not there and I cannot specify OR.
Hi,
Thank you for contacting us and I'd be happy to assist.
Your observation is correct and the option to choose between the 'AND' or "OR" relation between the multiple custom field query filters, is no longer available in the views.
We already have an internal ticket to improve this and I've added your voice to this matter as well. Although I don't have a time estimate to share at the moment, for now, a workaround can be to manually change the relation to "OR" for the custom field filters, using the "wpv_filter_query" hook.
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
You'll find the example code snippet in this forum reply:
https://toolset.com/forums/topic/view-that-uses-custom-fields-filters-also-in-multiple-taxonomies/#post-1886039
I hope this helps and please let me know if you need any further assistance with this.
regards,
Waqar
Hello Waqar
Many thanks for your reply, and the links to help with using the wpv_filter_query hook. I'll certainly try it, and get back to you if I need further assistance.
I'd only just noticed this problem, but it seems from the forum post you pointed to that it's at least two years old. Hopefully there'll be a fix soon!
Thanks
Julian
Hi Julian,
You're very welcome.
Please feel free to test the code and let me know how it goes.
regards,
Waqar
Hello Waqar
I've tried the wpv_filter_query hook in a Toolset custom code snippet, and as far as I've been able to test it, it seems to work fine. Very many thanks for your help.
The forum post you pointed me to also asked about more complex queries, combining multiple AND and OR clauses, such as <condition1> AND (<condition2> OR <condition3>). Is there a way to implement these more complex queries?
Regards
Julian
Thanks for the update and glad that it is working.
Combining multiple conditions in the meta query is possible and you just have to pass them in a proper array structure.
You'll find a good example of its usage in this official documentation of the WordPress:
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
Look at the last code example in that section:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'orange',
'compare' => '=',
),
array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => 'red',
'compare' => '=',
),
array(
'key' => 'size',
'value' => 'small',
'compare' => '=',
),
),
),
);
$query = new WP_Query( $args );
Hi Waqar
Great, thanks for your help!
Regards
Julian
My issue is resolved now. Thank you!