A Fix for a WordPress Paging Problem
Dr. B. | Friday, November 09, 2007
I recently discovered a problem in the next/previous paging links of a custom WordPress 2.2.1 site we created for Fractional Jets Focus. When viewing a list of articles by category, the previous posts link generated a 404 error.
After digging for too long through WordPress code and Google search results, I found that the problem was related to an apparent bug or conflict between the paging feature and custom permalinks.
Our custom permalink setting in Options -> Permalinks is this:
When you want to view all the posts for a certain category, an example URL is /blog/2007/9/. The problem is that the URL generated by
There is probably some voodoo I could have done in the .htaccess file with mod_rewrite to fix the issue, but I feared breaking something else. So, I wrote the following plugin. It's so short that I'm publishing the whole thing inline.
This plugin simply checks to see whether the post name is 'page', and if there is a 'page' parameter too. If so, it removes the 'name', and assigns the page index to the magic 'paged' parameter. Paging restored.
Doug Smith, Senior Developer, Barefoot
After digging for too long through WordPress code and Google search results, I found that the problem was related to an apparent bug or conflict between the paging feature and custom permalinks.
Our custom permalink setting in Options -> Permalinks is this:
/%category%/%year%/%monthnum%/%postname%/
When you want to view all the posts for a certain category, an example URL is /blog/2007/9/. The problem is that the URL generated by
next_posts_link()
was misinterpreted by WordPress because of the permalink. The link was: /blog/2007/9/page/2/
. Unfortunately, the string "page" in this URL was interpreted as a post name, instead of a token for the page index.There is probably some voodoo I could have done in the .htaccess file with mod_rewrite to fix the issue, but I feared breaking something else. So, I wrote the following plugin. It's so short that I'm publishing the whole thing inline.
<?php
/*
Plugin Name: Fix Paging in Category Listings
Plugin URI: http://www.thinkbarefoot.com
Description: Fixes a bug where next/previous links are broken in category by year/month listings
Version: 0.5
Author: Doug Smith
Author URI: http://www.thinkbarefoot.com
Copyright 2007 Doug Smith (email: dsmith@thinkbarefoot.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/**
* Function to fix problem where next/previous buttons are broken on list
* of posts in a category when the custom permalink string is:
* /%category%/%year%/%monthnum%/%postname%/
* The problem is that with a url like this:
*
* /category/2007/10/page/2
*
* the 'page' looks like a post name, not the keyword "page"
*/
function remove_page_from_query_string($query_string)
{
if ($query_string['name'] == 'page' && isset($query_string['page'])) {
unset($query_string['name']);
// 'page' in the query_string looks like '/2', so split it out
list($delim, $page_index) = split('/', $query_string['page']);
$query_string['paged'] = $page_index;
}
return $query_string;
}
add_filter('request', 'remove_page_from_query_string');
?>
This plugin simply checks to see whether the post name is 'page', and if there is a 'page' parameter too. If so, it removes the 'name', and assigns the page index to the magic 'paged' parameter. Paging restored.
Doug Smith, Senior Developer, Barefoot
25 comments
Hi,
Just have to say a huge thanks! The plugin works just as promised and solved the problem I had on two sites...
Ditto that. Thanks for sharing!
Yes -- thank you!
You are the Tylenol to my headache.
Thank you so much for this post. It just opened my eyes to something I've been overlooking for the past 2 months.
!
Yesss. That's it. Thank you so much. First for the great analyze. And after for the solution.
Hi. This looks promising, but not sure how to get this working on my blog with a permalink structure of:
/%category%/%postname%
The paging I wish is in the category pages. When I see the page:
/categoryname/page/2
The 404 error appears. What part of your code should I modify to get it working?
Thanks!
THANK YOU! They should give you a medal for this.
March 2009
I just upgraded to WP 2.7.1 and was having problems with custom permalinks set to /%category%/%post_id%
This plugin did not fix the problem, but I was able to modify the plugin as follows to fix my problem... hopefully this will help others!
--start php--
Plugin Name: Fix Paging in Category Listings
function fix_page_in_category_from_query_string($query_string) {
//Check to see if the 'p' and 'category_name' are set in $query_string
if ( isset($query_string['category_name']) && isset($query_string['p']) ) {
$category_name = $query_string['category_name'];
$category_len = $category_name;
//Check to see if the 'category_name' has '/page' on the end of it
if (substr($category_name, $category_len-5,5) == '/page') {
//Remove '/page' from the end of 'category_name'
$query_string['category_name'] = substr($query_string['category_name'],0,$category_len-5);
//Set 'paged' equal to the page you want to go to
$query_string['paged'] = $query_string['p'];
//Unset 'p' since we don't need it anymore because we set 'paged' instead
unset($query_string['p']);
}
}
return $query_string;
}
add_filter('request', 'fix_page_in_category_from_query_string');
-- end php --
Thanks for this - been looking for a fix for months!
Thank You very much! Works perfectly!
Thanks for sharing! I'm using the custom permalink structure '/%category%/%postname%/' with Wordpress 2.7.1... Tried numerous other solutions online, but this was the only one that worked for me.
I absolutely love you! been have a problem with this for months, Thank you so much!
Thanks! Just what I was looking for.
HOly f-ing shi* THis fixed it, OMG thsnk youuuuu
Thank you so much - this was driving me crazy. It's interesting that this has been an issue with WP for so long and hasn't been fixed yet.
Hi, my permalinks are %category%/%post-name% but I can't figured out how to make it work. The pagination didn't work and 'page' isn't removed from url.
Any help?
Thank you so much
Its working now with jsherk solution. Thank u so much!
Your plugin just saved the day. Thank you!
thank you very much!!!
Where does this code go ?
opps sorry got it ! made it in to a plugin ..
Unfortunately it doesn't seem to work :-(
I am using permalink structure ..
/%category%/%postname%
anything I'm doing wrong ?
Excuse the fluster..
Thanks, this works nicely. It seems to add a split second to rendering time for the page, though - any idea why?
Ah, nevermind. It was a "my bad". This is still a great fix, thank you.
Post a Comment
« Home