0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.