• I have posts block with relative “load more” button to load the following posts, the problem is that all duplicates are presented to me. I would like to add a function similar to this to the 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);
        }

    This is the post block nav.php file:

    <?php ?>
    <div class="<?php echo esc_attr( self::$id_base . '-nav' . " {$this->block_instance['settings']['navigation']}" );?>" data-role="navigation" data-nav="<?php echo esc_attr( $this->block_instance['settings']['navigation'] );?>" data-uid="<?php echo esc_attr( $this->uid );?>">
    <?php   break;  case 'load_more':   ?>
    <a href="javascript:void(0)" class="load-more" data-role="load-more" rel="nofollow" data-loading="<?php echo esc_attr_x( 'Load More', 'block navigation', 'wp-post-blocks');?>"><span><?php echo esc_html_x( 'More', 'block navigation', 'wp-post-blocks');?></span><?php echo apply_filters( 'wp_post_blocks/icon', '', 'nav_more'); ?></a>
    <?php   break;  default:    break;} $this->block_preloader();?></div>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the action “the_post” instead of “post_link”. “post_link” can fire more than once per post. Not the end of the world, but better avoided. You need some conditionals to ensure you are acting on the right query. Exclude admin queries for starters. There should be some distinct query vars you can check to ensure you’re acting on the right query.

    You have a good idea, keeping track of what has been output and exclude those posts from subsequent queries. What should be better is to keep track of how many pages or offset you are into the query. By “pages” I mean batches of posts, not literal pages. Every request gets you a “page’s” worth of posts. By paginating the query (but output on the same actual page), you avoid duplicates without having to track every single post. Just keep track of the last page’s worth of posts requested.

    Thread Starter marcorroma

    (@marcorroma)

    could you help me please? I would like to create a function but I can’t. Can you kindly give me some input?

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, that’s a bit much for me to do as a quick freebie. If you don’t know anyone who’s able to help you you could hire professional help through resources like https://jobs.wordpress.net/ or https://jetpack.pro/

    Just don’t try to solicit hires through these forums, it’s against our guidelines. I also advise that you disregard any unsolicited offers that might arise from this topic. While it’s likely just someone looking for extra work, there could be someone with malicious intent making similar solicitations.

    Thread Starter marcorroma

    (@marcorroma)

    Thanks for support

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Duplicated posts via “Load more” button’ is closed to new replies.