Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having trouble adding pagination to a page. I'm using WP_query and would like to pull back 4 posts per page. The basic query seems to work OK but I must be missing something regarding pagination.

// The query for 4 posts
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $query = new WP_Query();
            $query->query('showposts=4'.'&paged='.$paged);

if ( $query->have_posts() ) : 
    while ( $query->have_posts() ) : $query->the_post(); ?>
    <div class="post">
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      <p class="author">by <?php the_author(); ?></p>
    </div>
    <?php endwhile; ?>
    <!-- end of the loop -->
    <nav>
        <?php previous_posts_link('&laquo; Newer posts') ?>
        <?php next_posts_link('Older posts &raquo;') ?>
    </nav>

        <?php wp_reset_postdata();

        // If no results appear
        else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>

I've set the var for the pagination at the top as such:

$query->query('showposts=4'.'&paged='.$paged);

Am including the pagination nav as such:

<nav>
    <?php previous_posts_link('&laquo; Newer posts') ?>
    <?php next_posts_link('Older posts &raquo;') ?>
</nav>

But I just get empty HTML <nav></nav> - but no errors, and I can't work out what I am missing.

Edit: As per the suggestion I have updated as:

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'post_per_page' => 4,
            'paged'         => $paged,
        );
        $query = new WP_Query( $args );

and updated the button link to:

<?php next_posts_link( 'Older posts &raquo;', $query->max_num_pages ); ?>

I am still not seeing a pagination link.

share|improve this question
1  
Try this: next_posts_link('Older posts &raquo;', $query->max_num_pages) –  Howdy_McGee Sep 4 at 13:50
    
You might want to take a look at How to fix pagination for custom loops?. –  ialocin Sep 4 at 14:17

1 Answer 1

up vote 3 down vote accepted

Please do not use showposts it got replaced by posts_per_page ages ago.

Personally I would add the arguments to the WP_Query like shown below, additionally pagination should work like shown below:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post_per_page' => 4,
    'paged'         => $paged,
);
$query = new WP_Query( $args );

global $wp_query;
// Put default query object in a temp variable
$tmp_query = $wp_query;
// Now wipe it out completely
$wp_query = null;
// Re-populate the global with our custom query
$wp_query = $query;

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

    previous_posts_link('&laquo; Newer posts');
    next_posts_link( 'Older posts &raquo;', $query->max_num_pages );
    wp_reset_postdata();

else :
    // no post found code 
endif;

// Restore original query object
$wp_query = null;
$wp_query = $tmp_query;

Which is the same as the Q&A How to fix pagination for custom loops? I linked you to by @ChipBennett.

Another note, if this in a page template working as static front page, you have to use the query variable page instead of paged:

$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
share|improve this answer
    
Hi there. This is just within a standard page, not a static front page. Sadly the code you have provided does not work :( –  Francesca Sep 4 at 14:43
    
Your code should work +1 –  Pieter Goosen Sep 4 at 14:53
    
@Francesca Have you read the question I linked in my comment? Especially the »Hack the main query object« related part. –  ialocin Sep 4 at 14:57
    
Thanks, this does work. Apologies I didn't see your edit. –  Francesca Sep 4 at 15:47
    
My pleasure. Not a problem, besides at first I didn't add the »main query hack«, which is the reason it works now. @Francesca –  ialocin Sep 4 at 15:50

Your Answer

 
discard

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.