Skip Navigation

[Resolved] Remove duplicate posts in a many-to-many (M2M) View

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a nested View structure that loops over post type A in a M2M relationship, and displays all the post type B related posts for each item in the loop of post type A. Since it's an M2M relationship, some post type B are shown multiple times. I would like to filter out the duplicate posts.

Solution: There is not an easy way to do this with nested Views. A JavaScript solution can work, but only without pagination in the View. In the Loop Editor:

<wpv-loop>
<div class="remove-duplicate-stories">
  [wpv-post-link]
</div>
</wpv-loop>

In the JavaScript panel:

jQuery(document).ready(function(){
var seen = {};
jQuery('.remove-duplicate-stories').each(function() {
    var txt = jQuery(this).text();
    if (seen[txt])
        jQuery(this).remove();
    else
        seen[txt] = true;
});
});
This support ticket is created 6 years, 4 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by doronN-2 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#924174

Hey
Reposting a followup question to this

https://toolset.com/forums/topic/post-relationship-and-taxonomy-views/

We ended up using a many to many relationships i.e a destination has several stories and a story can relate to several destinations. For example, the story "10 grate hotels Israel" can be related to (Destination) Jerusalem and Tel Aviv.
Took (the very useful) steps you describe in your answer, but because the relationship is now many to many, ended up with:

Beach destinations
Cancun| Isla Mujeres

10 beach hotels in Mexico
Good places to eat in Mexico
10 beach hotels in Mexico

The "10 beach hotels in Mexico" is associated with Cancun and Isla Mujeres so it apres twice..

Is there a way to prevent this duplicity?

Thanks!

Doron

#924319

Hi, there's not an easy way to do this with M2M posts and nested Views. I can provide a solution that does something similar with custom JavaScript code, but it would only work if the View is not paginated. Wrap each Story loop item in a div with a special CSS class, then use jQuery to loop over all those items and remove duplicates after the page loads:

<wpv-loop>
<div class="remove-duplicate-stories">
  [wpv-post-link]
</div>
</wpv-loop>

Then in the JS panel of the Loop Output editor:
[php]
jQuery(document).ready(function(){
var seen = {};
jQuery('.remove-duplicate-stories').each(function() {
    var txt = jQuery(this).text();
    if (seen[txt])
        jQuery(this).remove();
    else
        seen[txt] = true;
});
});

Try that out and see what you think of the results.

#951181

That did the trick...
Thanks