Skip to content
Advertisement

WordPress pagination does not go to last page

I am trying to use a custom WP_Query for my loop in order to limit the number of posts that I show per page. However, the pagination functions doesn’t seem to respect this limit that I set. Here is my code:

$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;

$author_post_args = array(
    'post_type'      => 'post',
    'author_name'    => $curauth->user_nicename,
    'posts_per_page' => 5,
    'paged' => $current_page,
    'orderby'        => 'modified',
    'no_found_rows'  => true,
    'ignore_sticky_posts' => '1'
);

$loop = new WP_Query( $author_post_args );

$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $loop;

if($loop->have_posts()):
    while($loop->have_posts()): $loop->the_post(); ?>
        <div class="post-list">
            <?php 
            $title = the_title("", "", false);
            ?>
            <h2><a href="<?php the_permalink();?>"><?php echo $title; ?></a></h2>
            <time>Last Updated — <?php the_modified_date('F j, Y'); ?></time>
            <p><?php the_excerpt(); ?></p>
        </div>
<?php
    endwhile;
endif;

wp_reset_postdata();

previous_posts_link('<i class="fas fa-backward"></i> Newer Posts');
next_posts_link('Older Posts <i class="fas fa-forward"></i>', $loop->max_num_pages);

$wp_query = NULL;
$wp_query = $temp_query;

I searched for a solution on the website and found this question. The solution seems to work for the OP but not for me.

In my case, the posts per page are limited to 5. However, the number of pages is still stuck at 3. I can’t go to page 4 and get a Not Found error. There are total 26 posts by the author. Therefore, I should be able to go to page 6.

The number of pages is still determined by the functions based on the value specified under Settings > Reading > Blog pages show at most in WordPress dashboard which is set to 10.

What am I doing wrong?

Thanks.

Advertisement

Answer

The problem is that WordPress is working off of the assumption that you’re still expecting 10 posts per page because your query is effectively a standalone query in the middle of a page. Are you just trying to limit the number of posts per page on this particular post type? Or is there something else you’re trying to do?

You can achieve this in a far cleaner way by using pre_get_posts

function tweak_posts_per_page( $query ) {
    if( !is_admin() && $query->is_author() && $query->is_main_query() ) :
        $query->set( 'posts_per_page', 5 );
        $query->set( 'orderby', 'modified' );
        return;
    endif;
}

add_action('pre_get_posts', 'tweak_posts_per_page');

Add this code in your functions.php file or similar; if needed you can add additional conditionals to the IF statement to handle when this adjustment will be applied. You should find your pagination will work after that.

pre_get_posts is used to adjust the query that WordPress will run before it runs the query

Additionally, you can remove the query from your code altogether – just keep the loop in there to loop through what is now your adjusted main query.

edit: added the additional ‘is_author’ conditional statement, and added the ‘orderby’ adjustment

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement