• I would like to exclude from the “Load More” button or “Scroll infinity” posts already previously displayed on the current page. I use a post blocks which contains various categories ID, so I cannot use a simple offset.

    Nav.php https://textuploader.com/1fiiq

    Post block example: https://textuploader.com/1fiic

    To avoid duplicate posts in post blocks I use this, but it doesn’t work with the “Load More” or “Scroll infinity” button:

    add_filter('post_link', 'cr_search_displayed_posts');
    add_action('pre_get_posts','cr_remove_already_displayed_posts');
    
    $displayed_posts = [];
    
    function cr_search_displayed_posts($url) {
    global $displayed_posts;
    $displayed_posts[] = get_the_ID();
    return $url; // don’t mess with the url
    }
    
    function cr_remove_already_displayed_posts($query) {
    global $displayed_posts;
    $query->set('post__not_in', $displayed_posts);
    }

    Can someone help me? Thanks guys

The topic ‘Prevent duplicate posts in categories’ is closed to new replies.