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

Update May 6th, 2011: This article is now outdated, please read this one instead for a more updated way get your custom query working with pagination and 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:

  $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 ?>
  $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:

  $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 ?>
  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.

You may also like...

22 Responses

  1. Federico says:

    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 says:

    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:

    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 says:

    Dude! It works! You rock! Thanks!

  4. Hafiz Rahman says:

    Hey, glad to hear that!

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

  6. Hafiz Rahman says:

    Woo hoo! Glad it helps!

  7. Ilyon says:

    Sorry! I’ve read all your solutions about error 404 when using permalinks in page navigation, but don’t understand how to use $wp_query code. When I have to paste it or to replace the rows, which are? in which file???

  8. Roberto Costa says:

    Hi everyone,

    I’m relatively new to WP and I’ve been looking for a solution to this problem for quite a while now with no success yet.
    Hafiz, your suggestion works to the point of displaying my custom posts list and the wp-pagenavi links below but when I try to go to page 2, 3 … the same posts keep showing although the url shows (http://localhost:8888/wp3.0beta1/?page_id=100&paged=2).
    Other relevant details:
    I placed the query on a page template. Create a new page and chose this custom page as template. This page is not my front page. It’s accessed by a wp_page_list menu item.
    The posts I want to show and paginate are of a new post type that I created (wp 3.0). So I changed the query string from “new WP_Query(‘cat=…” to new “WP_Query(‘post_type=…”.
    During my tests I noticed it works with cat=, displaying regular posts. But it doesn’t help because I need to display my custom post type.
    Isn’t it possible to paginate custom post types? I don’t believe so. I prefer to believe it’s my fault.
    Can someone help me out of this problem? I’m almost throwing the towel.
    Thank you in advance.

  9. Giuseppe says:

    Thanks a lot for this.

  10. This works for me. I had a bit of a an issue like Roberto Costa, but it did work in the end. I had omitted the (…. &paged=’.$paged ) in the query.

    @ Roberto: check and see if you have included it. As soon as I put it, it worked like a charm.

    Thanks for the great article

  11. Joe Critchley says:

    Superstar. Thanks for the article. =)

  12. Nick says:

    Absolutely fantastic. It was the ‘paged’ variable I was missing to make it work with my paging functionality. Now it works a treat. Thanks for your short example, it really helped me find the solution quickly.

  13. ardi says:

    i found problem at paged when use wp_query()
    my pagination can’t work properly..
    another option else wp_query?

  14. Tony Blade says:

    Thanks for sharing! :) I wish I’ve had some time to share stuff too… I feel really obligated by people like You! ;)

    Cheers mate!

  15. Aaron says:

    When I added wp_pagenavi() to someones code, it wouldn’t work properly because of the custom variable.

    $recent = new WP_Query(‘custom_content’);


    $wp_query = $recent = new WP_Query(‘custom_content’);

    or preferably just

    $wp_query = new WP_Query(‘custom_content’);

  16. Patti says:

    I’m having the hardest time with this. My custom loops aren’t done the same way as I pull in meta values. Any ideas on that?

  17. kn00tcn says:

    @janvier, yep the paged part seems to be the thing that fixes everything, super

  18. Carolyn says:

    that was awesome, just really needed this (cat=-6&paged=’ . $paged) and the endwhile section with my query_post string and it solved me… I tried just replacing my code with yours (the easy way out) and it broke my page, but when I actually thought about what you were doing, this solved it for me… Really, you do rock!

  19. Thank you for this, its a life saver, there is a way that it can be implemented in every category, i mean i want this code in my category templates to display the category loop, but it has to be general for every category.

    (i am doing this because i have 2 loops, the first one shows the last post of the category with other style -featured- and then i have the second loop to show the rest of the post in a more discrete way, )

  20. Dan says:

    No need to do this is you just call your wp_query something else and send it as a param in page navi. that way you don’t mess with wp_query which can be messy.

    have_posts()) : $my_query->the_post();

  1. February 6, 2010

    […] 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 […]

  2. November 2, 2010

    […] following link helpful when working with template pages.. having pagination problem check this out VN:F [1.9.5_1105]Rating: 0 (from 0 votes) […]