Skip Navigation

[Resolved] Do in a view can I use filter #1 OR filter #2 instead filter# AND filter #2?

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

Problem:
The user has some taxonomy filter and would like to get the posts that match all of them.

Solution:
By default, Toolset will look for the posts that match any taxonomy condition. To achieve this, it will require custom code. Check this example:

add_filter( 'wpv_filter_query', 'func_change_filter_relation', 10, 3 );
function func_change_filter_relation( $query_args , $view_settings, $view_id ) {
    if ($view_id==1234) {
        if ( isset( $query_args['meta_query']['relation'] ) ) {
            $query_args['meta_query']['relation'] = "OR";
        }
    }
    return $query_args;
}

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

This support ticket is created 3 years 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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: Africa/Casablanca (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by andreaP-10 3 years ago.

Assisted by: Jamal.

Author
Posts
#2221375

Hi there,
I added a custom post type named "bandi" with a custom fields named "campi bandi" in witch I added a fiel named "data scadenza"
If I set a content template with a new view to show some of those posts, I can add any custom search filter I need but those filters use AND criteria
Is it possible to use OR criteria to show the result of filter #1 plus the result of filter #2 ?
Thank you so much

#2221445

Hello and thank you for contacting the Toolset support.

This will require custom code to change the query arguments, using the and apply an "OR" instead of an "AND".

add_filter( 'wpv_filter_query', 'func_change_filter_relation', 10, 3 );
function func_change_filter_relation( $query_args , $view_settings, $view_id ) {
    if ($view_id==1234) {
        if ( isset( $query_args['meta_query']['relation'] ) ) {
            $query_args['meta_query']['relation'] = "OR";
        }
    }
    return $query_args;
}

Change the view's ID(1234) on line 3 with your view's ID. If you are building the view using the legacy editor, you can take the ID from the URL. If the view is built using the blocks editor, you can check the browser devtools to get the ID of the view.

Read more about this filter here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

I hope this helps. Let me know if you have any questions.

#2221473

Hi Jamal,
thanks for your assistance. Exuse me but..where must I add the custom code?
Thanks
AP

#2221515

You can add it to the theme's functions.php file, or a plugin, or in Toolset->Settings->Custom code. In the last case, make sure that the code is active. Check this article https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
Check article too https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-use-toolset-api-hooks-in-custom-code/

#2221543

Ok Jamal. Done. A last question: Can I mix the AND and OR mode? In other therms, can I have a set of filters with "OR" mode and another set of filters with "AND" mode?
I deteils you. I have 5 filters: 3 based on custom taxonomy. for those I need an "AND" mode query. 2 based on "data scadenza". For those lasts I would like the customer choice if filtering the posts with "AND"or "OR" criteria. is it possible? If yes, how can I do this?
Tnx

#2221561

Well, it is not possible out of the box, or using the user interface. But with some custom coding you can achieve that. The trick is to change the query arguments using the wpv_filter_query. A view will use an WordPress query to get the posts from the database. Read more about its arguments here https://developer.wordpress.org/reference/classes/wp_query/

As you may notice, that's what does the code that I suggested earlier. It will change the relation of the meta_query parts of the query arguments.

#2222121

My issue is resolved now. Thank you!