Barefoot Development

A Fix for a WordPress Paging Problem

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:

/%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

  1. Blogger Unknown said:  

    Hi,
    Just have to say a huge thanks! The plugin works just as promised and solved the problem I had on two sites...

  2. Blogger Tzaddi said:  

    Ditto that. Thanks for sharing!

  3. Blogger Greg said:  

    Yes -- thank you!

  4. Blogger Sators said:  

    You are the Tylenol to my headache.

  5. Blogger Andy said:  

    Thank you so much for this post. It just opened my eyes to something I've been overlooking for the past 2 months.

    !

  6. Blogger Blogsahibi said:  

    Yesss. That's it. Thank you so much. First for the great analyze. And after for the solution.

  7. Blogger Shanx said:  

    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!

  8. Blogger Michelle McGinnis said:  

    THANK YOU! They should give you a medal for this.

  9. Anonymous Anonymous said:  

    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 --

  10. Blogger Dave Redfern said:  

    Thanks for this - been looking for a fix for months!

  11. Blogger vlatko said:  

    Thank You very much! Works perfectly!

  12. Blogger Ed Hope said:  

    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.

  13. Blogger Unknown said:  

    I absolutely love you! been have a problem with this for months, Thank you so much!

  14. Blogger Unknown said:  

    Thanks! Just what I was looking for.

  15. Blogger T.H.E. Truth said:  

    HOly f-ing shi* THis fixed it, OMG thsnk youuuuu

  16. Blogger Avi said:  

    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.

  17. Blogger Bibiana said:  

    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

  18. Blogger Bibiana said:  

    Its working now with jsherk solution. Thank u so much!

  19. Anonymous Anonymous said:  

    Your plugin just saved the day. Thank you!

  20. Blogger kaubonschen said:  

    thank you very much!!!

  21. Blogger Lawrence said:  

    Where does this code go ?

  22. Blogger Lawrence said:  

    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 ?

  23. Blogger Lawrence said:  

    Excuse the fluster..

  24. Blogger Nah said:  

    Thanks, this works nicely. It seems to add a split second to rendering time for the page, though - any idea why?

  25. Blogger Nah said:  

    Ah, nevermind. It was a "my bad". This is still a great fix, thank you.

Post a Comment

« Home