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(); ?>

1 Answer 1


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;

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.