I've taken the example straight from the codex on this one, and my pagination is broken.

Here's the codex page I copied/pasted the code from: https://codex.wordpress.org/Function_Reference/next_posts_link

Everything is working. My posts are showing, and I get only the next_posts_link, however it only works until page 2. The previous_posts_link never appears.

I can only paginate to page 2, and I cannot go back or further.

I'm using WordPress 3.5.2 and I have reset my permalinks after attempting this.

I put a little debug section below to find how many max pages there are, the var_dump returns float(87).

And here's my PHP code. Any thoughts?

function homepage_test() {
    // Set the limit of posts to show per page
    $limit = 5;
    // set the "paged" parameter (use 'page' if the query is on a static front page)
    $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;

    // the query
    $the_query = new WP_Query( 'showposts=' . $limit . '&paged=' . $paged . '&orderby=date&order=DESC' );

    if ( $the_query->have_posts() ) :
    // the loop
    while ( $the_query->have_posts() ) : $the_query->the_post(); 


    // usage with max_num_pages
    next_posts_link( 'Older Entries', $the_query->max_num_pages );
    //Debug to show how many max pages.
    previous_posts_link( 'Newer Entries' );

    // clean up after our query
    else: ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif;
  • 1
    Adjusting pagination in template is extremely unreliable. You need to be doing this in pre_get_posts hook or in othe way before template is reached.
    – Rarst
    Jul 29, 2013 at 14:07
  • And trying to paginate secondary queries with the ordinary *_posts_link functions is difficult if not impossible. Please take a look at the numerous "pagination" questions on the right under "Related".
    – s_ha_dum
    Jul 29, 2013 at 14:11
  • If it's difficult, why do they have it listed in the codex as an example (and even say, "set the "paged" parameter (use 'page' if the query is on a static front page)")? Most of the related posts deal with get_query_var( 'page' ), which I'm not having a problem with.
    – Pat
    Jul 29, 2013 at 14:43
  • @Rarst, I played with pre_get_posts, but it doesn't seem to be changing the outcome. Prev/Next links are still broken and/or only going to page 2. function homepage_posts( $query ) { if ( is_front_page() ) { $query->set( 'posts_per_page', 1 ); return; } } add_action( 'pre_get_posts', 'homepage_posts', 1 );
    – Pat
    Jul 29, 2013 at 15:00
  • Also, I installed wp_pagenavi, and that's also not working. I'm at a complete loss with the code above and getting a page navigation working for paginated static front page.
    – Pat
    Jul 29, 2013 at 15:11

1 Answer 1


previous_posts_link() echos the result of get_previous_posts_link() which checks the paged query variable, not the page query variable used on static pages. You will have to write your own code to display the link on a static page.

I didn't test it, but you might try fooling WordPress:

function homepage_test() {
    global $paged;

Now your changes to the $paged variable in your code will be seen by get_previous_posts_link(). I don't know what else it might affect or if the link will be correct.

  • As it looks right now, this seems to work great! (Won't know more until I get further into it, but at least I'm not stuck). Thanks!!
    – Pat
    Jul 30, 2013 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.