Skip Navigation

[Resolved] Set Query Filter with ‘AND’ and ‘OR’ condition in one query

This thread is resolved. Here is a description of the problem and solution.

Problem:

Setup custom field query filter with 'AND' and 'OR' condition in one query.

Solution:

There isn't such kind of built-in feature within Toolset, you can setup only one "Fields relationship" in the post Query Filter.

But it is possible with custom codes, for example, you can use filter hook wpv_filter_query to trigger a PHP function:

https://toolset.com/forums/topic/set-query-filter-with-and-and-or-condition-in-one-query/#post-1825715

Relevant Documentation:

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

This support ticket is created 4 years, 2 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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by roulartaM 4 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1825071
query-filter.png

Hi support,

I have a table called 'Wedstrijden' (English = 'matches'). All data in this custom post type is imported via WP All Import by an external XML.

There are 3 fields in this CPT that are relevant to my question:

- Datum (English = 'date')
- Thuisploeg ID (English = 'home team ID')
- Bezoekersploeg ID (English = 'away team ID')

These can be potential records in that CPT:

01/10/2020 - Knack Roeselare - Lindemans Aalst
10/10/2020 - Caruur Gent - Knack Roeselare
11/10/2020 - Tectum Achel - Lindemans Aalst
20/10/2020 - VC Greenyard Maaseik - Knack Roeselare
30/10/2020 - Knack Roeselare - Caruur Gent
09/11/2020 - Knack Roeselare - Tectum Achel
13/11/2020 - Caruur Gent - Lindemans Aalst
...

Now I want to make a view that get all the 'Knack Roeselare' matches in the future. So the above results should be minimized to these results:

30/10/2020 - Knack Roeselare - Caruur Gent
09/11/2020 - Knack Roeselare - Tectum Achel

It means that I should set up a view for this CPT that gets all results where:

1) My shortcode attribute called 'ploeg' is equal to 'Thuisploeg ID' (=> 'Knack Roeselare')
OR
2) My shortcode attribute called 'ploeg' is equal to 'Bezoekersploeg ID' (=> 'Knack Roeselare')
AND
3) 'Datum' is greater than NOW

The first 2 conditionals should be grouped so the query looks as follow:
(conditional 1 OR conditional 2) AND conditional 3

How can I make this work? In the query filter, I can only select one 'Fields relationship' between all conditionals. See screenshot.
If necessary, I can provide some additional login data.

Thanks for reading and helping,
Sam from Roularta

#1825715

Hello,

There isn't such kind of built-in feature within Toolset, you can setup only one "Fields relationship" in the post Query Filter.

But it is possible with custom codes, for example, you can use filter hook wpv_filter_query to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

In this PHP function, follow WP document to setup the custom query filters:
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
See the example codes in above document:
https://developer.wordpress.org/reference/classes/wp_query/#highlighter_805956

#1829765

Hi Luo,

Thanks for pointing me in the right direction, I've written my own query now. Here's the code, perhaps other people can take it as an example:

[code]
add_filter( 'wpv_filter_query', 'changeNextMatchFilter', 99, 3 );

/**
* @see https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
* @param array $query_args Argumenten zoals die momenteel in WP_Query zitten
* @param array $view_settings View instellingen
* @param int $view_id View ID
* @return array $query_args Argumenten die de WP_Query klasse vanaf nu zal gebruiken
**/
function changeNextMatchFilter ($query_args, $view_settings, $view_id) {
if ( $view_id == 1902 ) {
$thuisploeg = $query_args['meta_query'][0]['value'];
$bezoekersploeg = $query_args['meta_query'][1]['value'];

$new_query_args = array(
'post_type' => 'wedstrijd',
'post_status' => array('publish', 'private'),
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'wpcf-datum',
'value' => time(),
'compare' => '>',
),
array(
'key' => 'wpcf-postponed',
'value' => '0',
'compare' => '=',
),
array( //https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
'relation' => 'OR',
array(
'key' => 'wpcf-thuisploeg_id',
'value' => $thuisploeg,
'compare' => '=',
),
array(
'key' => 'wpcf-bezoekersploeg_id',
'value' => $bezoekersploeg,
'compare' => '=',
)
)
),
'order' => 'ASC'
);

return $new_query_args;
}
return $query_args;
}
[/code]

Kind regards,
Sam from Roularta