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.

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
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.
Dude! It works! You rock! Thanks!
Hey, glad to hear that!
YES!!!!! I’ve been working in a custom theme and had some problems with wp-postnavi, now everything is solve!!! thx :)
Woo hoo! Glad it helps!
[...] 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 [...]
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???
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.
Thanks a lot for this.
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
Superstar. Thanks for the article. =)