Interested in purchasing one of the best wordpress themes in town? check out Cult on themeforest »

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['paged'];
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!


    About the author

    My name is Philip Beel. I have four years commercial experience in front end web development. My disciplines include XHTML, CSS, PHP, MYSQL, Smarty and javascript. I am also a keen advocate of the jQuery framework.

    Read more posts by


    38 Comments on 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



    16. hossein

      tnx , it helpd me a lot :D

      « Reply



    17. merry

      I am new with WordPress. Where should i put this code on?

      I had included this code “” in the footer.php of my theme but not sure how to link the posts.

      « Reply

      philip beel Reply:

      @merry, It depends on the construction of your theme, I would advise putting it in loop.php

      « Reply


    18. Yuda

      You saved my life Philip Beel, thank yous so much.
      Anyway, When I first put this code:
      $page = $_GET['page'];
      to my script, it didn’t work. Then I tried with this
      $page = $_GET['paged'];
      since the parameter passed is named that way, and it worked. Just in case someone having the some problem like I did.
      Thanks again

      « Reply

      philip beel Reply:

      @Yuda, Thanks for the heads up, I will update my code!

      « Reply


    19. deecoup

      Great helping resources for me so i m saying thanks to whom write this post

      « Reply


    20. saintist

      $query = $GLOBALS['wp_query'];
      $paged = $query->get( ‘paged’ );

      « Reply


    21. Fernando

      THANKS!!! I LOVE YOU :)

      You Saved ME!!

      « Reply


    22. Karlo Estrada

      Oh man! Can’t thank you enough for this solution. You just nailed it…just one shot and that’s it. Simple!

      « Reply


    23. Julio Edgardo Girón

      Maaannn!!! you are the best and saved my life!!!! I was looking for that solution.Thank you so much.

      « Reply


    24. Huroman

      It works like a charm. Dude, you’re like… Chuck Norris. Thank you for the GREAT tip.

      « Reply


    25. daniel mamann

      Hello! I know this is kind of off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!

      « Reply


    26. andrei.moonm

      wow, Thanks for this; I was looking for a solution for about 2 days now

      « Reply


    Leave a comment


    This pretty much personifies me - http://t.co/glu8rD8j #cycling

    Follow me