Hi,
I am trying to set a filter on a view where all posts are supposed to be shown, EXCEPT for posts that are related to one post. So basically using "wpvrelatedto", but then "wpvnotrelatedto". Is there a way to that? Because I see this option when filtering on a tag, category, etc. but not in the relationships.
Please help 🙂
Kind regards,
Tim
Hi Tim,
Thank you for contacting us and I'll be happy to assist.
Your observation is correct and there is no direct option available for selecting "not related to" posts.
But you can invert the result of a "wpvrelatedto" query filter, by adding the following code, in the active theme's "functions.php" file:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
add_filter( 'wpv_filter_query', 'filter_invert_related_fn', 1000 , 3 );
function filter_invert_related_fn( $query_args, $view_settings ) {
if ( (!is_admin() && isset($view_settings['view_id'])) && ($view_settings['view_id'] == 123) ) {
if (array_key_exists('post__in', $query_args)) {
$query_args['post__not_in'] = $query_args['post__in'];
unset($query_args['post__in']);
}
}
return $query_args;
}
Note: Please replace 123 with the actual ID of the view, where you'd like to use this code.
After that, when you'll set a relationship filter based on a related post's attribute, it will act conversely.
I hope this helps and please let me know how it goes.
regards,
Waqar