Skip Navigation

[Resolved] How to remove duplicates in multi relationship views

This support ticket is created 7 years, 2 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 7 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#479464

I'm trying to figure out how to remove duplicates in a multi relationship view.

Organizer, Exhibitors, Markets
Intermediary object = Bookings

I want to create a view where an "Organizer" can see all "Exhibitors" (that has ever booked one of the organizers "Market").

I create a view listing all "Bookings" ....
(Select posts that are a children of the current post in the loop – "Organizer")

The problem is that this gives me duplicates of "Exhibitor" as they may have booked multiple times before.

How do I filter out the duplicates so an Exhibitor is only listed one time?

#479884

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tina

If you have a query which is "show all Bookings with a parent of the current Organizer" and there are multiple Bookings for a given Exhibitor then there is no way to say avoid duplicate exhibitors.

We need to come at this from a slightly different angle, using a View that lists Exhibitors which will loop over all of them.

In the Loop Output section we won't output anything directly. The output is going to come from a View that we nest inside this View.

The nested View will query Bookings, and in the loop output section it will output not the title of the matching booking, but of the parent Exhibitor using the id attribute. This View for Bookings needs query filters for the post parents, one to specify the Exhibitor, and one to specify the Organizer.

Unfortunately Views will only let you add one parent post filter, even though you can have multiple parent post types, and so we will need a little custom code to add a second filter to the query.

So here's my set up:

- I have a Content Template for single Organizer posts which is where I want to display Exhibitors that have booked with this organizer
- To that Content Template I add a View which queries all Exhibitor posts with no query filters required
- In the Loop Output section of that View I simply insert another, nested, View
- This View queries Bookings. Make sure the option "Don't include current page in query result" is set.
- Set the Limit to 1 (this will stop us getting duplicates of the Exhibitors)
- Add a Post relationship query filter that specifies "Select posts that are a children of the current post in the loop." This will select Bookings which are children of the current Exhibitor from the outer View.
- This is where we also need to add a filter to only include Booking posts that are children of the Organizer we are currently viewing. (I'll come back to how in a moment.)
- So this View will get Booking posts which are child posts our Organizer and the current Exhibitor we are looping through, and to return a maximum of one such post.
- If it finds a matching Booking, we want to output the title of the Exhibitor, so in the Loop Output section add [wpv-post-title id="$exhibitor"] (adjust the slug as required).
- If there is no matching booking for this Organizer-Exhibitor combo we don't want to output anything, so in the Loop Output section make sure the [wpv-no-items-found] shortcode block is empty.

Now, we will need some code to add the Organizer filter to the query. You'll need to add the following to your theme's functions.php file or using a plugin like Code Snippets.

function mod_view_query( $view_args, $view_settings, $view_id ){

	if ( 220 == $view_id ) {

		$oid = $view_args['post__not_in'][0]; // Organizer id
	
		$view_args['meta_query'] = array(
			array(
				'key'	=>	'_wpcf_belongs_organiser_id',
				'value'	=>	$oid,
				'type'	=> 'CHAR',
				'compare'	=> '='
				)
		);
	}
	return $view_args;
}
add_filter( 'wpv_filter_query', 'mod_view_query', 101, 3);

Please be sure to edit the View id (this is the id of the inner view), and the _wpcf_belongs slug.

I'm employing a little trick here. We need to pass the id of the Organizer we are concerned with, and as I assume we are viewing a single Organizer post, checking the "Do not include the current page" box in the view makes the id available in $view_args.

Let me know how you get on.

#479943

WOW
How much time did you spend on giving me this very amazingly extensive answer.
You Yoda ...

I'll get back to you when I manage to follow through your instructions.

super exited 🙂

#479947

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Too long 😉

I was sure there was a clever solution that didn't require resorting to writing any additional code, but in the end I had to give in.

Let me know how you get on.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.