WordPress Pagination With WP-PageNavi & query_posts()

wordpress pagenavi tutorial

Updated: A better method has been described for dealing with this issue, by simply using get_query_var(‘paged’) in place of curPageURL().

I recently worked on a project, which required me to use wordpress to create a blog. One of the objectives was to break the blogroll up into catagories using multiple templates and the custom query_posts() function. This in itself was fine, however I ran across a problem when I wanted to use Lester ‘GaMerZ’ Chan‘s wordpress WP-PageNavi plugin to paginate the posts over multiple pages. This post will talk you through how to resolve this issue and be able to use the WP-PageNavi plugin with any query_post() parameters and proper permalink structures.

Whats the problem!?

The problem comes when WP-PageNavi is used in conjunction with query_posts() and custom permalink structures. Bascially the plugin cannot determine which page the user is on, therefore it defaults to the first page every time the page loads.

Fixing without custom permalinks

If your blog is not using a custom permalink structure and you have opted for “ugly” URL’s you can quickly bypass this issue with the following code:

$page = $_GET['page'];
query_posts('cat=1,2,3&paged='.$page);

All we are doing here is assigning the page number that is passed as a GET parameter into a variable called $page. Then in the query_posts() function we use the assigned variable to dynamically draw out the correct page number and thus displaying the correct page.

Fixing it with a custom permalink structure

If your using a custom permalink structure you will inevitably run into the issue that there are no GET parameters being passed for the page number. Lets consider the following URL of a page on your blog:

url_startbar

In order to work out what page we are on we will need to strip off everything from this URL except for the page number, in this case 2. In order to achieve this I have modified a PHP funciton written by web master cheat sheet. Which looks like this:

function curPageURL() {
 $pageURL = 'http';
 //check what if its secure or not
 if ($_SERVER["HTTPS"] == "on") {
 $pageURL .= "s";
 }
 //add the protocol
 $pageURL .= "://";
 //check what port we are on
 if ($_SERVER["SERVER_PORT"] != "80") {
 $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 //cut off everything on the URL except the last 3 characters
 $urlEnd = substr($pageURL, -3);
 //strip off the two forward shashes
 $page = str_replace("/", "", $urlEnd);
 //return just the number
 return $page;
 }

The function curPageURL() basically gets the URL of the page you are on, then removes everything but the current page number and assigns it to a variable called $page ready for use in our query like so:

//add in the catagory and page number
query_posts('cat=7,8,12,13,15&paged='.curPageURL());

As you can see the query is almost identical to beofore, only this time we call the function instead of the variable. Job done!

  • Digg
  • StumbleUpon
  • del.icio.us
  • Twitter
  • Google Bookmarks
  • email
  • Facebook
  • RSS
Theme forest

21 Responses to “WordPress Pagination With WP-PageNavi & query_posts()”

  1. Rogers Sampaio

    In a moment of despair, I found the light.

    Thank you for that fix.

    « Reply

  2. Juan

    Nice solution man! Thanks ^^!

    « Reply

  3. Duncan Michael-MacGregor

    Thank you so much for this!
    I couldn’t work out how to fix the problem and accidentally found your blog (via wpengineer.com write-up on your contact form) and BAM! job done.

    Lovin’ this sites design btw! :)

    « Reply

  4. Duncan Michael-MacGregor

    Oh I also wrote up my experience on wordpress.org to help others in need of this fix with a link to this page.
    See: http://wordpress.org/support/topic/363101

    « Reply

  5. Fatih Bektaş

    Problem is solved. Thank you. But
    query_posts ( ‘cat = 7,8,12,13,15 & paged =’. curPageURL ());

    As the name of the category adlandıryor title. How can we solve the problem. :( :(:

    « Reply

  6. Phatty Matty

    Thanks for offering this solution – it works great. I am using a custom permalink structure but rewrote your curPageURL using a regular expression match. It reduces the code to two lines. The regular expression is searching for any number of digits after “page/. This is what I came up with:

    function curPageURL() {
    preg_match(‘/page\/(?P\d+)/’, $_SERVER["REQUEST_URI"], $matches);
    return $matches['page'];
    }

    Thanks again!

    « Reply

    Phatty Matty Reply:

    Looks like some of the code was stripped out. This part ‘/page\/(?P\d+)/’ should have a less than sign followed by “page” (no quotes) followed a greater than sign. This should come immediately after “?P”. So: (?P[less than sign]page[greater than sign]\d+) – of course you need to substitute [less than sign] and [greater than sign] with their appropriate character.

    « Reply

  7. Adam

    Oh my gosh. I can’t believe how simple this solution was. Thank you; I was pulling my hair out and now the pagination on the site I was working on works perfectly!

    « Reply

  8. Bali

    Thank you so much, I can do custom permalink for plugins URI because your tip after three months trial and errors. You Save my hours!

    « Reply

  9. svenl77

    Tanks! :-)

    « Reply

    svenl77 Reply:

    @svenl77, my spell checker makes Tanks out of Thanks….

    « Reply

  10. scribu

    Instead of curPageURL() you can use get_query_var(‘paged’);

    « Reply

    philipbeel Reply:

    @scribu, Thanks for the tip!

    « Reply

    KJuly Reply:

    @scribu, The tip is really good! Thx!

    « Reply

  11. Satılık Yat

    Nice solution man! Thanks. How can I add #top at the end of link.

    « Reply

  12. James

    Sorry if this is a dumb question, but where should I insert the code above? funcitons.php? index.php? And where in those files?

    « Reply

  13. Yahya

    Hi! I’m using your solution which is awesome and destress my nerves.

    However, I could not get my query_posts orderby and order to work properly. I’m not sure why.

    Can I put my code here for you to see? Or you can go to http://www.singanista.com/demosite/english-s-o-s/

    Plz help me.

    « Reply

  14. DK

    Hi, Thank You Very Much for this great solution.

    « Reply

  15. Travis Pflanz

    This did not work for me. I am using WP-pagenavi 2.7.2 and WordPress 2.9.2

    Please send me a message through my website http://oneroyalway.com/about-one-royal-way/contact-one-royal-way

    « Reply


Talk to me