Skip Navigation

[Resolved] Using AND – OR logic on custom search

This support ticket is created 5 years, 6 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 replies, has 3 voices.

Last updated by Franck 5 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#1241057

Hi,

i use a big query on my website, and i have a question about what i can do or not

On my picture you can see i have different queries on field with "and" operator and i use also query on taxonomy with "or" operator.

It works, but i have other taxonomies which need "and" operator so i can not add, i have to convert to field if i want to query on it.

My question : Is there a solution to have more than two groups like today (field and taxonomy)

#1241063

Hi, I'm not able to see your picture but it sounds like you want to have complex groups of conditionals, including some taxonomies with AND and some taxonomies with OR. Complex grouped conditions are not possible in the Views Query Filter editor. The best way to accomplish complex groups of conditions is to use the Views API to manipulate query filters using your own custom code. We have documentation about that API available here: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Here are some examples of complex taxonomy term filter structures:
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

#1241069
Capture d’écran 2019-05-08 à 17.29.41.png

Sorry, i give you my picture

But,

Do you think i could use front view and add custom code ?
And, it is the same with custom post field, we can manage with custom code ?

#1241078

I'm sorry, I don't understand the question. Could you explain the question a bit more?

#1241110

I mean, right know i use Toolset to make filter, html output ... so when you advice to use api, i have to filter the id view ? but i continue to use toolset for front ?

With api we can make custom complex groups of conditionals taxonomies but also custom fields ?

#1241192

Yes, you will continue to use Toolset Views to display the results on the front-end of the site. The wpv_filter_query API allows you to create your own custom query by manipulating the WP_Query arguments. I'm not an expert with these queries, so I'm not sure exactly what types of combinations are supported by WordPress, but here's an example of someone else's nested taxonomy query:
https://wordpress.stackexchange.com/a/310245

#1242300

Hi, thanks a lot, i have made the code bellow. And it is working.

Do you think, you could make something like this to make join queries or nested queries ?
I explain : Right now i have two custom post types (parent and child) Like Home and Room. I have to filter both in the same time, so to do that i have created relationship fields and i fill it on the room cpt, even if it concerns the home custom post type.

It works, but for me it not logical and clean, the clean thing would be to make join queries or nested queries.

----

My code

function add_city_tax( $query_args, $view_settings, $view_id ) {
if($view_id == 7706) {
//ville versaille = 158
//ville hyeres = 766
//CP 83400 = 1156
//CP 78000 1155
$args = array(
array(
array(
'taxonomy' => 'ville',
'field' => 'id',
'terms' => array( 766 ),
'operator' => 'IN',
),
array(
'taxonomy' => 'code-postal',
'field' => 'id',
'terms' => array( 1155 ),
'operator' => 'IN',
),
'relation' => 'OR',
),
array(
array(
'taxonomy' => 'dispositif',
'field' => 'id',
'terms' => array( 11 ),
'operator' => 'IN',
),
'relation' => 'OR',
),
'relation' => 'AND',
);
}
$query_args['tax_query'] = $args ;
$msg = '<pre>' . print_r($query_args, true) . '</pre>';
//mail('franck@efficonex.fr', 'test ok 8', $msg);
return $query_args;
}
add_filter( 'wpv_filter_query', 'add_city_tax', 99, 3 );

#1243070

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Romane,

As christian is currently on vacation I will be handling his queue for him.

Taking a look at your code, i don't see anything wrong with it. As you say it works and nesting your individual taxonomy queries in the arrays is actually the correct way to do it as this is how they do it in the wordpress query doc.

Thanks,
Shane

#1245668

My issue is resolved now. Thank you!