Skip Navigation

[Resolved] Use wpvrelatedto to select posts NOT related to

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

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/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 6 years ago.

Assisted by: Waqar.

Author
Posts
#1178961

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

#1179165

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