• Hi!

    I have several pages on my site. Each of them displays posts belonging to a certain category. For example, the “Recipes” page displays only posts from the “Recipes” category, the “Jokes” page displays only posts from the “Jokes” category, and so on. The main page displays posts from all the categories. Suddenly I noticed that there are duplicate posts on the main page. For example, the “Recipe 1” post appears there 2 times. Both of these posts have same address. Interestingly, these posts are not repeated in the “Recipes” category. They are only repeated on the main page. Why does it happen? I tried different themes but no luck. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey mate!

    If WordPress matches the criteria for multiple loops. WordPress will display it again unless you exclude it, and this required editing the theme code by temporarily storing the data about the posts displayed in the first loop. Once we have that information, we’ll simply modify our second query to exclude those posts from reappearing.

    Firstly, you need to make a backup for your website, look for functions.php and paste this code at the bottom:

    <?php
    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);
    }

    Have a good day

    Thread Starter basil555

    (@basil555)

    Thank you for your reply, but, unfortunately, it didn’t help. I still get duplicate posts.

    Hello, can you please provide the URL of your website so can take a look?

    Thread Starter basil555

    (@basil555)

    Hello,

    As it turned out, the problem was appearing only in Chrome, even after I cleared the cache. All other browsers were unaffected. I still have to figure out why does it happen, but this is not a big deal now ?? Thank you very much for your kind help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Duplcate posts on the main page’ is closed to new replies.