Prevent duplicate posts in categories

  • 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

  • Hi there,

    Your site is not hosted on WordPress.com, but using the open source WordPress.org software at another hosting provider.

    Please contact support for the theme you’re using directly for help with this.

    For general help with the version of WordPress you’re using, you can ask the WordPress.org community over at https://wordpress.org/support/forums/

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