Skip Navigation

[Resolved] Many to many relationship posts repeating in the loop

This support ticket is created 6 years, 1 month 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
- 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+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by Nigel 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#624791
nearby-locations.jpg

Hello,
So i've built a many-to-many relationship between multiple posts (Bike Tours, Hotels, Restaurants, Services, etc.) called "Nearby location". I am supposed to display custom posts that are "near each other".
The functionality is implemented, but i am having a little problem. If posts are assigned multiple times in the front end (situation that i can't have control over), the nearby posts are also duplicating.

I've followed the Toolset documentation for many-to-many replationship guide.
So "Nearby location" post types has as parents all the post types mentioned above.

I will attach a image on how the result looks at the moment and also provide the code used in the view for generation this result.

Content Selection: Nearby locations
Query Filter: Post relationship filter -> Select posts that are a children of the current post in the loop.


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<h1 class="nearby-titles">[wpml-string context="Nearby" name="FOOD & DRINK ON THE ROAD"]FOOD & DRINK ON THE ROAD[/wpml-string]</h1>
	<wpv-loop>  
      	[wpv-conditional if="( $(_wpcf_belongs_restaurant_id) ne '' )"]
      <div class="nearby-single-block" onclick="getUrl('section-[wpv-post-id id='$restaurant']', 'cat-[wpv-post-type id="$restaurant"]');">
      		<span>[wpv-post-title id="$restaurant"]
              	<div class="clear"></div>
        		[wpv-conditional if="( $(wpcf-meat-food-type).id(restaurant) eq '1' )"]
				<img src="/wp-content/uploads/2017/11/cow-white.png" />
				[/wpv-conditional]
				[wpv-conditional if="( $(wpcf-fish-food-type).id(restaurant) eq '1' )"]
				<img src="/wp-content/uploads/2017/11/fish-white.png" />
				[/wpv-conditional]
              	[wpv-conditional if="( $(wpcf-vegetables-food-type).id(restaurant) eq '1' )"]
				no image(vegetables)
				[/wpv-conditional]
				[wpv-conditional if="( $(wpcf-vegetarian-food-type).id(restaurant) eq '1' )"]
				<img src="/wp-content/uploads/2017/11/mushroom-white.png" />
				[/wpv-conditional]
				[wpv-conditional if="( $(wpcf-vegan-food-type).id(restaurant) eq '1' )"]
				no image(vegan)
				[/wpv-conditional]
        	</span>              	
			[wpv-post-featured-image id="$restaurant" size="custom" width="250" height="250" crop="true"]
      </div>
      	[/wpv-conditional]            	
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No nearby restaurants[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

My question: Is there any way I can prevent the posts from duplicating even if there are assigned multiple times in the front end? Or I have implemented the relationship wrong.

Thank you.

Link to the site: hidden link

#624857

Nigel
Supporter

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

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

Hi Hendrik

I think the solution here is to prevent the duplicates being created in the first place.

Can you remind me how the nearby location intermediate posts are being added? Directly in the back end, or via a CRED form.

If you could elaborate a little on the set up for creating these posts I'll think about how to prevent duplicate submissions.

#625091
many-to-many relationship posts.jpg

Hello Nigel,
So the nearby location posts are added in the backend something like this (attached image).
Also highlighted how the duplicates happen.
When nearby posts add up, it's pretty hard to keep track on what's duplicated and what's not. Also I think that the case of reciprocity creates this issue too. ( If a hotel is nearby a restaurant, and the same hotel is nearby an activity...then the hotel is also near the activity ). I'm not sure if that's the problem but i am thinking something like "check for post ID in a IF loop" 😕

Thank you for the support.

#625296

Nigel
Supporter

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

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

Hi Hendrik

OK, thanks for clarifying.

This is a bit tricky.

As you probably know Types is being updated to overhaul how post relationships work. Please be aware that having multiple post types share a single intermediary post type won't be possible in the new version, so when you upgrade Types in the future and see that you have the option to switch to the new version of post relationships, do not use it. Your site will continue to work fine with the current system.

That said, solving the problem of duplicates is tricky.

What you need it in the post edit screen from your screenshot, the list of available parent posts needs to be filtered so that previously selected parents cannot be added again. But there is no filter available to control that list of parents.

I've done something similar with CRED form parent selectors before, but it requires a certain amount of detective work to identify which queries are being used to populate the select options.

When I have a little time I will get on to it, but please bear with me.

#626677

Nigel
Supporter

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

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

Hi Hendrik

Sorry for the delay getting back to you, it was a long weekend here in Ireland.

I was just going through this now but I'm not sure that the current set up makes sense, and in the absence of of an API filter to modify these lists of parent posts makes it very difficult to isolate the queries that retrieve them.

You are using one intermediate post type (locations) to connect many different post types (restaurants, apartments, tours, activities, attractions etc.) but the number of posts that can be connected to each other is not fixed, e.g. 1, meaning that the location posts are entirely arbitrary (and which contributes to your duplication problem).

For apartment A, for example, you might connect 4 restaurants, 2 tours, 3 activities and one attraction.

In any given location post you can only connect a maximum of 1 of each type of post. So to apartment A with a first location intermediate post you could connect restaurant 1, tour 1, activity 1, and attraction 1.

Or you might just as easily connect restaurant 3, tour 1, activity 2, and attraction 1.

Or restaurant 2, no tours, no activities, and attraction 1.

As I say, these are entirely arbitrary and therefore meaningless.

I'm going to suggest you split your post relationships, so that you have one relationship (with intermediate post type) for connecting apartments to restaurants, another to connect apartments to tours, another to connect apartments to activities, and another to connect apartments to attractions.

Then, on the apartment edit screen you can add nearby restaurants (via its intermediate post type), nearby tours, nearby activities, and nearby attractions.

You will then solve the problem of people adding duplicates because, well, it's obvious if they are adding nearby restaurants and they add the same restaurant twice.

You will also need to create the other relationships, e.g. restaurants near to attractions etc., and that implies some effort to update the post relationships and the Views which display related posts, but

- your relationships will have logic and the connections will make sense which should fix your duplication issue,
- you will be able to migrate to the new post relationships, and
- the UI for users will then improve further because they will be able to directly connect, e.g. apartments to restaurants, with the intermediate post becoming invisible to them, and
- I think the current set-up makes it too difficult to extract the correct queries with pre_get_posts allowing for all the different combinations and the different queries that are run on page refresh versus ajax updates for the parent selectors when there are many posts to choose from

Thoughts?

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