May 21st by Neil under Hints and Tips, Wordpress
Holy smokes, well this took me around 2 hours to sort out so Im writing it all down so I can save people some time and remember it myself!!
Right OK if you are diving into WP you will undoubtably want to try some wonderful things with the loop, if like me to display posts from a certain category on a page. Well its pretty easy to do but doing so breaks any pagination you may have i.e. WP-Pagenavi or posts_nav_link(). Whatever you are using it will not work if using a custom loop containing a query_posts() or $wp_query() function.
A lot of people will tell you it cant be done and just to use showposts to display all your posts or there is a few blogs out there sporting fixes which dont work….I dont know if they ever have or if its using a different version or if I had missed something (as others said it worked for them but I tried lots of variations on the theme). Anyhoo I finally picked up on a couple of comments, I combined two ideas and hey presto it worked….I couldnt believe it.
So here it is, this is what your loop should look like:
query('cat=20&paged='.$paged); ?>
....loop content
....alternative content
This line is key and a few other fixes out there use it with query posts like this:
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; query_posts(‘cat=3&showposts=12&paged=’.$paged);
That didnt work for me and I picked up on a comment suggesting that wp_reset_query() wouldnt work with query_posts(), then I found another comment posting a different solution utlising wp_query->query(). Well it was 1am and I thought ‘ah well nothing to lose’ and it worked. Well Im very relieved to have found a solution that works for me and I hope it save you time and works for you too.
Oh yeh and remember if you have adapted loops on other pages, always include wp_reset_query(); at the end of the loop on each page.
Share and Enjoy!
Related Posts:
- Login directly from a page in WordPress using a sidebar login form
- Display your WordPress posts on other pages
- Page Templates in WordPress
- Using multiple/different headers, sidebars, footers and comment templates in WordPress
- Hide WordPress Update Nag Without a Plugin
- Advanced Nested Comments on WordPress Pages Issue Fix


Thank you Neil,
Could you please provide me with a working example. This is what I have tried so far:
/* demonz: filter */
$author_query = new WP_Query();//$args
//Create a new filtering function that will add our where clause to the query
function filter_where($where = ”) {
//posts in the last 30 days
$where .= ‘ AND post_author = ‘.$authid.”‘”;// get posts from author
return $where;
}
// Register the filtering function
add_filter(‘posts_where’, ‘filter_where’);
// Perform the query, the filter will be applied automatically
//query_posts($query_string);
//$paged = true;
$author_query->query(‘&post_status=publish&order=DESC&posts_per_page=5&paged=’.$paged);
… then
have_posts()) : ?>
have_posts()) : $author_query->the_post(); ?>
blah blah blah
<?php
if (is_paged()) {
if (function_exists(‘wp_pagenavi’)) { wp_pagenavi(); }
} else {
print 'not paged!’;
}
?>
What youve posted looks completely different from my solution and incomplete, perhaps some of it was removed by WP?
Anyway I have used this method on a few websites, it works with WP2.9.2 I havent tried it with WP3 yet.
Try the loop code I have posted above.
Wow… your rest query really saved me. I couldn’t figure out for the life of me what I was forgetting. Aside from that query, probably sleep. Thanks, man!
Holy Smokes is RIGHT! I really want to swear here. I’m using a front end post for a highly interactive site and I thought for sure that was breaking pagination.
and to be clear, you need `wp_reset_query();` at the very end of EVERY loop in your WP installation?
And it works on 3.1 FWIW. This one had be totally bewildered. Thanks.
Excellent, thanks for letting me know. Glad it helped.