WordPress pagination bug: the second page shows the same posts as the first
A common pagination bug in custom loops: the page links look right, but page 2, page 3 and so on show exactly the same posts as page 1. The fix is one missing argument in the WP_Query call.
Problem: A custom WP_Query loop outputs correct pagination links, but clicking “Page 2” loads the same posts as page 1.
The broken query
This loop builds the pagination from the current page number, but never tells the query which page to load:
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'order' => 'DESC',
'orderby' => 'date',
);
$query = new WP_Query( $args );
$total_pages = $query->max_num_pages;
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
// … output
endwhile;
if ( $total_pages > 1 ) : ?>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => get_pagenum_link( 1 ) . '%_%',
'format' => 'page/%#%',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $total_pages,
'prev_text' => '',
'next_text' => '',
) );
?>
</div>
<?php endif;
wp_reset_postdata();
endif; ?>
Without the paged argument, WP_Query always returns the first page, whatever page the URL points to.
The fix: pass the paged argument
Read the current page number before building the arguments and pass it to the query:
<?php
$paged = max( 1, get_query_var( 'paged' ) );
$args = array(
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'date',
);
Use the same $paged for 'current' in paginate_links(), so the highlighted page and the loaded posts always match.
The static front page uses “page”
If the loop is on the page set as the static front page in Settings → Reading, WordPress stores the page number in the page query variable instead of paged. A version that works on both:
$paged = max( 1, get_query_var( 'paged' ), get_query_var( 'page' ) );
Page 2 returns a 404
A related problem: the second page doesn’t show the same posts but a 404 error. This happens on archive pages when the custom loop has a different posts_per_page than the main query: WordPress checks the page number against the main query, finds no such page and returns 404. Instead of a separate WP_Query, change the main query with the pre_get_posts action, so there is only one query and one page count.