Pagination stops at page 6

The loop below works fine, apart from the fact that the pagination will always stop on page 6. No matter what arguments i specify, it will never show more than 6 pages.

Does anyone know why?

<?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;            
    $idxargs = array(
        'posts_per_page' => '1',
        'paged' => $paged,
    );

    $idxloop = new WP_Query( $idxargs );                   
?>

    <?php if ($idxloop->have_posts()) : while ( $idxloop->have_posts() ) : $idxloop->the_post(); ?>                  
        <?php the_title(); ?>                    
    <?php endwhile; ?>

   <?php next_posts_link( __( 'Next') ); ?>
   <?php previous_posts_link( __( 'Prev' ) ); ?>

    <?php endif; ?>
    <?php wp_reset_query(); ?>

2 Answers
2

Trying to use pagination from a different query is always fraught with potential disaster. next_posts_link accepts a second argument, $max_pages, which may solve your issue if you pass it properly:

next_posts_link( __( 'Next'), $idxloop->max_num_pages );

However, the real answer to this question is to adjust your query before the template. Adjusting the default query via a new WP_Query or by using query_posts is quite simply doing it wrong, despite the millions of examples on the web you will see that do exactly that.

The preferred method for this is using a pre_get_posts action along with a conditional check to apply it to the specific type of query you want to adjust:

function wpa64918_homepage_posts_per_page( $query ) {
    if ( $query->is_home() && $query->is_main_query() )
        $query->set( 'posts_per_page', 1 );
}
add_action( 'pre_get_posts', 'wpa64918_homepage_posts_per_page', 1 );

Leave a Comment