How can pagination in WordPress be improved to display older posts automatically?

To automatically display older posts when a user reaches the end of a paginated list in WordPress, you can implement infinite scrolling. This feature will load more posts as the user scrolls down the page, providing a seamless browsing experience.

// Add this code to your theme's functions.php file
function load_more_posts() {
    $args = json_decode( stripslashes( $_POST['query'] ), true );
    $args['paged'] = $_POST['page'] + 1; 
    $args['post_status'] = 'publish';

    query_posts( $args );

    if( have_posts() ) :
        while( have_posts() ): the_post();
            // Display your post content here
        endwhile;
    endif;

    die;
}

add_action('wp_ajax_load_more', 'load_more_posts');
add_action('wp_ajax_nopriv_load_more', 'load_more_posts');