WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having difficulty getting posts_nav_link() to display anything. Below is my index.php template file, I'm using permalinks (index.php/2011/08/26/sample-post/ format) and I've currently got my Blog pages show at most set to -1.

Does anyone know why the links aren't working?


    $args = array('posts_per_page' => 4,'paged' => get_query_var('page'));
    $myquery = new WP_Query( $args );

    <?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>

        <h1><?php the_title(); ?></h1>

        <?php the_content()?>

    <?php endwhile ?>

    <?php posts_nav_link() ?>

    <?php wp_reset_postdata(); ?>

<?php get_footer(); ?>
share|improve this question

You need to do a little "hack" to get pagination to work for your custom loop.

After you define $myquery, do the following:

// globalize $wp_query
global $wp_query;
// copy $wp_query into a temporary variable
$temp_wp_query = $wp_query;
// nullify $wp_query
$wp_query = null;
// move $myquery into $wp_query
$wp_query = $myquery;

At this point, your posts_nav_link() should work as expected.

Now, after the loop, swap the original object back into $wp_query, so that everything else on the page that is query-dependent will work properly:

// restore original $wp_query
$wp_query = $temp_wp_query;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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