
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:

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!

December 3rd, 2009
In a moment of despair, I found the light.
Thank you for that fix.
« Reply
December 11th, 2009
Nice solution man! Thanks ^^!
« Reply
February 11th, 2010
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
February 11th, 2010
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
February 17th, 2010
Very good! Thanks.
« Reply
February 17th, 2010
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
February 18th, 2010
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:
February 18th, 2010 at 9:49 pm
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
March 8th, 2010
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
March 22nd, 2010
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
March 26th, 2010
Tanks!
« Reply
svenl77 Reply:
March 26th, 2010 at 4:56 pm
@svenl77, my spell checker makes Tanks out of Thanks….
« Reply
March 26th, 2010
Instead of curPageURL() you can use get_query_var(‘paged’);
« Reply
philipbeel Reply:
March 27th, 2010 at 1:54 pm
@scribu, Thanks for the tip!
« Reply
KJuly Reply:
August 24th, 2010 at 4:09 pm
@scribu, The tip is really good! Thx!
« Reply
April 5th, 2010
Nice solution man! Thanks. How can I add #top at the end of link.
« Reply
April 10th, 2010
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
April 20th, 2010
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
May 25th, 2010
Hi, Thank You Very Much for this great solution.
« Reply
June 8th, 2010
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
June 8th, 2010
Found a much easier method here: http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html
« Reply
December 9th, 2010
tnx , it helpd me a lot
« Reply
December 15th, 2010
Legend. Good work.
« Reply
December 18th, 2010
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:
December 18th, 2010 at 3:46 pm
@merry, It depends on the construction of your theme, I would advise putting it in loop.php
« Reply
January 11th, 2011
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:
January 11th, 2011 at 9:24 am
@Yuda, Thanks for the heads up, I will update my code!
« Reply
March 6th, 2011
Great helping resources for me so i m saying thanks to whom write this post
« Reply
March 24th, 2011
$query = $GLOBALS['wp_query'];
$paged = $query->get( ‘paged’ );
« Reply
March 31st, 2011
THANKS!!! I LOVE YOU
You Saved ME!!
« Reply
April 1st, 2011
Oh man! Can’t thank you enough for this solution. You just nailed it…just one shot and that’s it. Simple!
« Reply
April 10th, 2011
Maaannn!!! you are the best and saved my life!!!! I was looking for that solution.Thank you so much.
« Reply
May 6th, 2011
It works like a charm. Dude, you’re like… Chuck Norris. Thank you for the GREAT tip.
« Reply
May 29th, 2011
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
September 10th, 2011
wow, Thanks for this; I was looking for a solution for about 2 days now
« Reply