Hello Toolset Support Team,
I have a View that lists therapists. Each therapist can have several custom fields for certifications ("Nachweise"). Each certification has a year field (for example: nachweis-basiskurs-jahr, nachweis-therapiekurs-1-jahr, etc.).
The View should only show therapists if all their certifications are still valid, meaning the year of the certification is greater than 2020. If any of the year fields is 2020 or lower, the therapist should be excluded from the results.
When I add multiple filters in the View for each custom field, they are combined with AND, which means the query only includes therapists that have all fields greater than 2020.
But in my case I need the opposite logic: as soon as one field is less than or equal to 2020, the therapist should be excluded. In other words, I need an OR combination of filters instead of AND.
I have checked the Toolset documentation on Views filters, conditions, and the wpv_filter_query hook. So far I only see options with AND logic, not OR.
Here is my View:
hidden link
Could you please let me know if there is a built-in way in Views to combine filters with OR logic, or if I need to use the wpv_filter_query hook to customize the query? Ideally, I would like to exclude therapists automatically if any of their certification year fields is less than or equal to 2020.
Thank you very much for your help!
Hi,
Welcome to Toolset support. The Views GUI cannot combine multiple custom-field filters with OR. To get "exclude a therapist as soon as any certification year is 2020 or lower," use the wpv_filter_query hook to build a meta_query that mixes AND and OR.
A starter code that you can use:
add_filter( 'wpv_filter_query', function( $query_args, $view_settings, $view_id ){
if ( (int) $view_id !== 12345 ) {
return $query_args;
}
$cutoff = 2020;
// Put your Toolset field slugs here (as stored in DB).
$fields = array(
'wpcf-nachweis-basiskurs-jahr',
'wpcf-nachweis-therapiekurs-1-jahr',
'wpcf-nachweis-therapiekurs-2-jahr',
// ...add more if needed
);
// Start or extend the meta_query
if ( empty( $query_args['meta_query'] ) ) {
$query_args['meta_query'] = array( 'relation' => 'AND' );
} else {
$query_args['meta_query']['relation'] = 'AND';
}
foreach ( $fields as $key ) {
// For each field: allow if NOT SET OR > 2020.
$query_args['meta_query'][] = array(
'relation' => 'OR',
array( 'key' => $key, 'compare' => 'NOT EXISTS' ),
array( 'key' => $key, 'value' => $cutoff, 'type' => 'NUMERIC', 'compare' => '>' ),
);
}
return $query_args;
}, 120, 3 );
Replace 12345 with your View ID and adjust the field slugs. This version keeps therapists when a given cert field is either missing or has a year > 2020; if a field exists and is ≤ 2020, the therapist is excluded.
You can add your custom code to Toolset > Settings > Custom code:
https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code
For more information about the filter_query:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Thanks.
Thank you very much.
I now implemented the code with my view-id and all the field slugs.
When I activate the code it filters but it shows only around 500 posts. But it should be around 1000 posts after the filter.
I don't know if it is because the fields aren't number-fields but simple text-fields. Or if it is because of the AND conditions.
The goal is that it should know all posts >=2020 or > than 5 years from the moment now.
Can you help me again?
Hi,
Would you please tell me if the field that you compare against is possible to have no value? Or 0? Or maybe non-numeric value such as: 2021/22?
You will need to give me some more information on which fields and how you set them up so I know how it should work.
Also, I'd appreciate it if you could give me the URL/User/Pass of your WordPress dashboard after you make sure that you have a backup of your website.
It is absolutely important that you give us a guarantee that you have a backup so if something happens you will have a point of restore.
Make sure you set the next reply as private.
Tell me where to check the view you added
I was able to solve it on my own, thank you very much.