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!

12 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 [...]

  7. Ilyon

    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

    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

    Thanks a lot for this.

  10. JanvierDesigns

    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

    Superstar. Thanks for the article. =)

Leave a Reply

Latest Links More →

Custom Shortlinks for WordPress

Have your own short domain name for the purpose of shortlinking? Here’s an easy way to combine that with your WordPress install.

The Quick Start Guide to Using Google Webmaster Tools With WordPress

GWT is a great, frequently updated features like showing you search queries volume, malware and crawl error diagnostics and links to your site. If you don’t use it yet, you probably need to. This will help you get started.

WordPress & jQuery Contact Form without a Plugin

I would recommend this either if you want more flexibility or to learn how to code a contact form.

Understanding and cleaning the pharma (spam) hack on WordPress

How to fix that hack:

This attack is very interesting because it is not visible to the normal user and the spam (generally about Viagra, Nexium, Cialis, etc) only shows up if the user agent is from Google’s crawler (googlebot). Also, the infection is a bit tricky to remove and if not done properly will keep reappearing.

Web Safe Fonts Cheat Sheet

An updated (written in April 2010), well researched, CC-licensed Web safe fonts cheat sheet, available both in low-res PNG and high-rest PDF. Even the article is useful as well.

The Nicest 2010 Child Theme You’ll See Today

The Timaru Mental Health Support Trust website, made for charity by Team USA (comprised by web superstars like Jason Santa Maria, Dan Mall, Liz Danzico and Automattic’s John Ford) during the FullCodePress competition, is actually a clever child theme of 2010.

More recap by JSM, Daniel Mall, and Liz Danzico.

WordPress 3.0 Theme Tip: The Comment Form

The simpler way to code comment form (once you understand how hooks and filters work).

Showing and hiding content with pure CSS3

I like it, I think it’s short and easy to understand.