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?