Site logo

[Support request] Avoiding duplicate posts on homepage

Home Forums Support [Support request] Avoiding duplicate posts on homepage

Home Forums Support Avoiding duplicate posts on homepage

  • This topic has 1 reply, 2 voices, and was last updated 5 years ago by Elvin.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1707112
    Serg

    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!

    #1707250
    Elvin
    Staff
    Customer Support

    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.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.