Skip Navigation

[Resolved] Exclude duplicate entries in a view

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

Problem: I have a View that shows a list of posts in a custom post type. Some of my posts have the same title. I would like to remove the duplicate titles from the View. How can I filter by unique post title?

Solution: There is no simple way to filter duplicate titles from wp-admin. You could implement your own custom code that uses the wpv_filter_query API or the wpv_filter_query_post_process API to filter out matching titles, or you can use this jQuery snippet to hide them on the frontend:

jQuery(document).ready(function(){
var strs = [];
var dupes = [];
jQuery('.wpv-loop').find('li > a').each(function(index,item){
  var txt = jQuery(item).text(); if( strs.indexOf(txt) > -1 ) { 
    dupes.push(index);
  } else{ 
    strs.push(txt);
  }
});
dupes.reverse();
for(var i=0; i<dupes.length; i++) {
  jQuery('.wpv-loop').find('li').eq(dupes[i]).remove();
}
});

Note that the duplicates may appear momentarily on the front-end until the JavaScript has been executed.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

This support ticket is created 6 years, 9 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 4 replies, has 2 voices.

Last updated by Matthias Reichl 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#620892

Tell us what you are trying to do?
++++++++++++++++++++++++++++
I have a view showing products of a supplier. (linked via a taxonomy with the supplier name)
Products, however, can exist multiple times.
In the view I want to show only the first appearance.

Look at this view: hidden link
here you see a lot of duplicate entries

Is there any documentation that you are following?
++++++++++++++++++++++++++++++++++++++++++
No, I only found a support topic with a similar, yet more complex case.
https://toolset.com/forums/topic/how-to-remove-duplicates-in-multi-relationship-views/

How could I solve it?

#621071

Well the easiest way to set up that type of filter is to use a custom field or taxonomy term on each post to indicate whether or not it is a duplicate of another post. Then add a filter to exclude all the posts with that custom field value or term. I'm not sure how your posts are created, but it may be possible to automate the custom field or term with some custom code. If posts are created with CRED, I can try to help you set up that type of automation.

#621239

Hi Christian!

I am importing these posts from another source and I fear I have no chance to recognize duplicates on import.
Isn't there a chance to make a duplicates filter with a code snippet similar to the one I mentioned in my original post?

#621817

The other ticket's approach works because the posts that would appear multiple times in the View are not just similar, they are identical - i.e. the same post with the same ID. Your duplicate posts are not identical. They have different post IDs, even though their post titles match. There is no simple method available here to filter out matching post titles using Views filters. With custom code, you can manipulate WP queries and results with our wpv_filter_query or wpv_filter_query_post_process APIs:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

On the other hand, if you want to manipulate the list of results on the front-end after the View is displayed using JavaScript to hide matching posts, you can try adding this code to the View's Loop Output editor JS panel:

jQuery(document).ready(function(){
var strs = [];
var dupes = [];
jQuery('.wpv-loop').find('li > a').each(function(index,item){
  var txt = jQuery(item).text(); if( strs.indexOf(txt) > -1 ) { 
    dupes.push(index);
  } else{ 
    strs.push(txt);
  }
});
dupes.reverse();
for(var i=0; i<dupes.length; i++) {
  jQuery('.wpv-loop').find('li').eq(dupes[i]).remove();
}
});
#622843

Hi Christian!

Thank you very much, it worked like charm!
Unlike Mr. Trump you are really a "stable genius"