# 756

How to get Custom WP_Query Loop Working with Pagination and WP-PageNavi

For this current redesign of wplover (have you seen it? come take a look!) I’m using a custom loop in the index page to remove all posts under “Links” category from the main content area. Now the most common problem with WP_Query-based custom loops is that it screws up pagination. No problem, we have this WBLT post to the rescue, and now my code looks like this:

<?php
  $temp = $wp_query;
  $wp_query= null;
  $wp_query = new WP_Query('cat=-6&paged=' . $paged); // don't show posts from category ID 6, a.k.a Links
  while ($wp_query->have_posts()) : $wp_query->the_post();
?>
 
<?php // the usual post-displaying codes here ?>
 
<?php
  endwhile;
  $wp_query = null; $wp_query = $temp; ?>

One more thing to add is that I’m also using the WP-PageNavi plugin to show custom page navigation after the posts. After messing around with the code a bit, I find that to get the plugin working, the plugin function call needs to be placed right after endwhile; and before $wp_query = null, like so:

<?php
  $temp = $wp_query;
  $wp_query= null;
  $wp_query = new WP_Query('cat=-6&paged=' . $paged);
  while ($wp_query->have_posts()) : $wp_query->the_post();
?>
 
<?php // the usual post-displaying codes here ?>
 
<?php
  endwhile;
  if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
  $wp_query = null; $wp_query = $temp; ?>

Placing the plugin call there will result in the correct pagination. You’ll get funky paging errors if it’s placed after the whole $wp_query variable swapping in the end.

No related posts found!

7 Responses to “How to get Custom WP_Query Loop Working with Pagination and WP-PageNavi”

  1. Federico

    Hi… Thanks for your tip. I have a problem and maybe you could help me. I’m doing a wp project. Everything works fine. $wp_query call done like you did. Using also wp_pagenavi. Now If I take out 404.php, I can go to page 2, I see the content of page 2 but says “page not found” on page title and also can’t validate html through w3c (it says page not found). Now I put 404.php back and now I don’t even see page 2 cause it launchs 404 page. I’m lost. If you can have a min to give a check let me know via mail and Ill send you the url. Thanks in advance, Federico

  2. Hafiz Rahman

    Hey Frederico, I just searched around for something similar to your issue and here are some that I found:

    404 Error on Previous Entries and Sub-Pages, Custom Permalinks:
    http://wordpress.org/support/topic/260214

    Using WP_Query() to make custom queries within WordPress templates:
    http://return-true.com/2008/12/using-wp_query-to-make-custom-queries-within-wordpress-templates/

    I don’t know why WordPress would behave like that, but on that last link it is suggested to try messing around with .htaccess, you might want to read and try it.

  3. Jackson

    Dude! It works! You rock! Thanks!

  4. Hafiz Rahman

    Hey, glad to hear that!

  5. Gaston Suarez Duek

    YES!!!!! I’ve been working in a custom theme and had some problems with wp-postnavi, now everything is solve!!! thx :)

  6. Add Post Pagination support to the WP Query Loop | Visser Labs

    [...] has a great WordPress article, How to get Custom WP_Query Loop Working with Pagination and WP-PageNavi, which steps through adding support for post pagination (e.g. Previous Posts, Earlier Posts) to [...]

Leave a Reply