• Hi. I’ve created a loop with pagination, to show three posts per page. The pagination is only showing two pages of posts, whereas it should be showing four, as there are 11 posts altogether. The code that I’m using is taken from the WordPress Codex. Any help would be appreciated. This is what I have at the moment:

    <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array(
        'posts_per_page' => 3,
        'paged' => $paged
    );
    
    $the_query = new WP_Query( $args ); 
    ?>
    
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>
    <?php endwhile; ?>
    
    <!-- pagination -->
    <?php next_posts_link(); ?>
    <?php previous_posts_link(); ?>
    
    <?php else : ?>
    <!-- No posts found -->
    <?php endif; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Hopefully Michael’s $max_pages solution (first link) will solve this for you. Generally speaking, you are better off managing your own pagination when using custom queries that instantiate new WP_Query objects. See Making Custom Queries using Offset and Pagination. For default pagination functions to work with custom queries, you should alter the default query through “pre_get_posts” action.

    The basic problem is you are using your own query, but “stealing” the page argument from the main default query. That’s fine as far as your query goes. The pagination functions think you are still using the default query though, so are working off the wrong set of parameters. Sometimes $max_pages will fix it. Other times it’s more complicated. I say handle your own pagination, it’s not that difficult ??

    !– pagination –>
    <?php next_posts_link(); ?>
    <?php previous_posts_link(); ?

    these functions are nowhere aware of your pagination limits.
    Your are just passing to a query.

    So logicaly this will not work.

    Thread Starter Jason_K70

    (@jason_k70)

    Thank you all for your responses. Michael, I integrated the $max_pages parameter, from the link you posted, but the issue remained. The link you posted, bcworkz, seems to explain the problem. I removed the posts_per_page argument that I was using and everything worked fine. It appears to be that this offset that was causing the problem.

    So right now it works, although my current number of posts per page is the default 10, which I can handle for now. I’ll look into using pre_get_posts, though.

    Thanks again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Loop with pagination only showing two pages’ is closed to new replies.