Skip Navigation

[Resolved] Query filter on two or more custom fields: AND / OR option not available

This support ticket is created 2 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 7 replies, has 2 voices.

Last updated by Creme 2 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2485631
or-query1.jpg
or-query2.jpg

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.

#2486143

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

#2486265

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

#2486267

Hi Julian,

You're very welcome.

Please feel free to test the code and let me know how it goes.

regards,
Waqar

#2487253

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

#2487727

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 );

#2487737

Hi Waqar
Great, thanks for your help!
Regards
Julian

#2487739

My issue is resolved now. Thank you!