Archived topic
Avoiding duplicate posts on homepage
1 reply · Started by Serg on March 23, 2021
Hello,
I'm working on transitioning an existing magazine site to GP (using Disptach). Most posts have multiple categories, and that's leading to repetition on the homepage when using WP Show Posts to display different categories.
I searched the forum and found that someone that had asked the same question about a year ago, and the response was that the only way to avoid this was to assign a single category to each post. Sooo, I'm wondering whether a different solution for this has surfaced over the past year or so, and hoping the answer is "yes" ... this isn't really an issue for business/organization websites, but it definitely is for magazine sites and even blogs, especially existing ones.
Thanks in advance!
Hi there,
Try this PHP snippet.
add_filter('post_link', 'track_displayed_posts');
add_action('pre_get_posts','remove_already_displayed_posts');
$displayed_posts = [];
function track_displayed_posts($url) {
global $displayed_posts;
$displayed_posts[] = get_the_ID();
return $url; // don't mess with the url
}
function remove_already_displayed_posts($query) {
global $displayed_posts;
$query->set('post__not_in', $displayed_posts);
}
Let us know how it goes.