Duplicate posts in loop

Question

how can I exclude posts already on the page?
Currently this code excludes only posts already present and generated by this php function.
I wish posts were not duplicated when I click the “Load more posts” button.

        /**
     * Post Query
     *
     * @param   $instance
     * @version 1.0
     * @since   1.0
     * @return  void
     * @return  array
     */
    public function the_loop(){

        $args = $this->block_instance['args'];
        $settings = $this->block_instance['settings'];

        if( !isset( $GLOBALS['wp_post_blocks_listed_posts'] ) )
            $GLOBALS['wp_post_blocks_listed_posts'] = array();

        $posts = $this->get_posts();

        if( empty( $posts ))
            return $this->listed_posts;

        // Backup global post
        global $post, $pbs_block;

        $pbs_block = static::$react;
        $temp_post = $post;
        $count = $flag = 0;

        // Misc settings for template()
        $misc = array(
            'count'         => 0, // total posts count per batch
            'flag'          => 0, // increment counter, reset on batch
            'ppp_flag'      => 0, // increment counter, reset on every static::$posts_per_page
            'order'         => 0, // increment counter for paged, won't be reset
            'is_paged'  => false,
        );

        if( !empty( $this->block_instance['args']['post__not_in'] ) && defined( 'DOING_AJAX' ) && DOING_AJAX ){
            $misc['order'] = count( $this->block_instance['args']['post__not_in'] );

            // If current viewing single post in the list, minus by 1
            if( !empty( $this->block_instance['settings']['post_id'] ) 
                && in_array( $this->block_instance['settings']['post_id'], $this->block_instance['args']['post__not_in'] ) ){
                
                $misc['order'] = $misc['order'] - 1;                    
            }
        }

        $misc['total']  = intval( count($posts) );
        // limit total if we have more posts than $args['posts_per_page']
        if( $misc['total'] > $args['posts_per_page'] )
            $misc['total'] = intval( $args['posts_per_page'] );

        $in_the_loop = $GLOBALS['wp_query']->in_the_loop;

        if( $in_the_loop )
            $GLOBALS['wp_query']->in_the_loop = false;

        // Push queried ids for nested queries to avoid duplicated posts
        $queried_ids    = wp_list_pluck( $posts, 'ID' );
        $misc['ids']    = $queried_ids;

        do_action( 'wp_post_blocks/render_template_loop_start', $settings, $misc );

        $this->render_template_loop_start( $settings, $misc );

        foreach ( $posts as $post ) : setup_postdata( $post );
        
            // This is for jetpack
            if( !empty( $settings['unique_posts'] ) && $post->ID == $temp_post->ID )
                continue;
        
            $GLOBALS['in_pbs_loop'] = true;

            $misc['count']++;
            $misc['flag']++;
            $misc['ppp_flag']++;
            $misc['order']++;

            if( in_array( $this->block_instance['settings']['navigation'], array( 'prev_next', 'prev_next_top' ) ) 
                && intval( $misc['order'] ) > intval( $misc['total'] ) ){
                $misc['is_paged'] = true;
            }

            do_action( 'wp_post_blocks/before_render_template', $settings, $misc );
            
            $this->render_template_before( $settings, $misc );
            // Render post template
            $this->render_template( $settings, $misc );

            $this->render_template_after( $settings, $misc );

            do_action( 'wp_post_blocks/after_render_template', $settings, $misc );

            // Push listed posts to array.
            if( !empty( $post->ID ) && !in_array( $post->ID, $this->listed_posts ) ){
                $this->listed_posts[] = $post->ID;
            }
            if( !empty( $post->ID ) && !in_array( $post->ID, $GLOBALS['wp_post_blocks_listed_posts']) )
                $GLOBALS['wp_post_blocks_listed_posts'][] = $post->ID;

            if( static::$posts_per_page == $misc['ppp_flag']){
                $misc['ppp_flag'] = 0;
            }
            // reset flag
            if( $args['posts_per_page'] == $misc['flag']){
                $misc['flag'] = 0;

                // Break the page if reached flag, for anonymous post!
                break;
            }

        endforeach;

        $this->render_template_loop_end( $settings, $misc );

        do_action( 'wp_post_blocks/render_template_loop_end', $settings, $misc );
        
        $GLOBALS['in_pbs_loop'] = false;

        wp_reset_postdata();

        if( $in_the_loop )
            $GLOBALS['wp_query']->in_the_loop = true;

        // Restore global post
        $post = $temp_post;

        // Return listed_posts
        return $this->listed_posts;
    }

Can someone help me?
Thank you

0
marcorroma 4 months 0 Answers 10 views 0

Leave an answer