Why is pagination not working? (7 posts)

  1. Adam
    Posted 3 years ago #

    Can some one tell me why on this piece of code the pagination doe not work?

    by "does not work" I mean that if you go to the previous page, the posts on the page before are the exact same. No matter how far back you go they remain the same. Its like the query doesn't update.

    The query in this case being used is:

    $query = array('posts_per_page' => 20);

  2. Sridhar Katakam
    Posted 3 years ago #

    Try changing it to

    $query = array('posts_per_page' => 20, 'paged' => $paged);

  3. Adam
    Posted 3 years ago #

    I did with no change in result. I have seen that trick before and alas it is not working. Any other ideas?

  4. keesiemeijer
    Posted 3 years ago #

    Maybe this will work:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $wp_query = new WP_Query(array('posts_per_page' => 20, 'paged' => $paged));

    On what theme template file are you doing this. There are more ways you can query the loop:

  5. Sridhar Katakam
    Posted 3 years ago #

    Also a recent article I came across covers this.

    #5 at http://kovshenin.com/2013/better-wordpress-theme-development/

  6. Adam
    Posted 3 years ago #

    @keesiemeijer again I have already tried that, as stated in the post above your reply. This is just being used in index.php, nothing fancy.

    Again the query is:

    @query = array('posts_per_page' => 20);

    I chose this query to test out pagination. See me as an example. Click "previous" and "next" to see that the posts are all the same.

    This would either indicate the query based on this code is not working as expected or is not resetting it's self properly.

    srikat this hasn't really helped me, thanks. I am developing a web based framework for theme development, so most of what you wrote is already covered.

    Any ideas? I have tried wp_reset_query(); and the trick I am doing now. I have tried the "$paged trick."

  7. keesiemeijer
    Posted 3 years ago #

    If this is on your front page you could try with this in your functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
          $query->set('posts_per_page', 20);
    add_action( 'pre_get_posts', 'my_post_queries' );

    Remove the query from your index.php and use a normal loop:

    next_posts_link('« Older Entries');
    previous_posts_link('Newer Entries »');

Topic Closed

This topic has been closed to new replies.

About this Topic