Explanation and workaround for error 404 on category pagination
-
I’ve found many many wp users with this exact problem in the last 3(!) years and now I’m facing it on WP 3.2.1 also. I’ve also found so many fixes and workarounds but not a clear explanation of the bug and its reproducibility, nor a simple and elegant solution. So here are my thoughts about it.
If you set a posts_per_page value in a template file, lower then the one specified in the backend, you get a 404 error on pagination. If I’m not wrong, the 404 error shows on the last page (the 2nd when you have only few posts).
After some debugging, I think the problem is that the var posts_per_page get lost going to page 2 and so WP uses the default one to decide if there are posts to show or not.
Some examples:
—————————————————————-
SET A GREATER VALUE
a) you have 9 posts in a particular category;
b) you have “5” as posts_per_page setted in the backend;
c) you specify “7” in your template file;First page:
WP reads the template file, set the var with the new value (7) so it shows posts from 1 to 7Second Page:
when you click on “next page”, WP lost your new posts_per page value and thinks: “ok, I’ve got 9 posts in this category, I’m on page 2 and I have to show 5 post per page, so this page exists for sure and I must load posts from 6 to 9!”
Then it loads the template file, your var is setted again to 7, page number is 2, so WP loads and shows (correctly) posts 8 and 9.
No more post to show so you don’t have a next page link to go further.SET A LOWER VALUE
a) you have 9 posts in a particular category
b) you have “5” as posts_per_page setted in the backend
c) you specify “3” in your template fileFirst page:
wp reads the template file, set the var with the new value (3) so it shows posts from 1 to 3.Second Page:
when you click on “next page”, WP lost your new posts_per page value and thinks: “ok, I’ve got 9 posts in this category, I’m on page 2 and I have to show 5 post per page, so this page exists for sure and I must load posts from 6 to 9“
Then it loads the template file, your var is setted again to 3, page number is 2, so WP loads and shows (correctly) posts from 4 to 6 and a link to the next page, cause not all posts has been showed.Third page:
when you click on “next page”, WP lost (again!) your new posts_per page value and thinks: “ok, I’ve got 9 posts in this category, I’m on page 3 and I have to show 5 post per page, so this page can’t exist!!!”
Then it loads the 404 template file, your are very angry ang go out for a walk!
—————————————————————-So, if you set a value greather then the one specified in the backend, even if the variable get lost and the default is used, WP calculates (mistaken for excess) that there are certainly some other posts to show. This is not problematic, as the code on template file corrects it and doesn’t allow to go to next page if all posts are showed.
If you specify a lower value, WP is “consuming” your remaining posts faster then your template file, which prints a link to next page and you get a 404 error.Ok. that’s a funny story. And now? My workaround is:
Set your default posts_per_page in the backend as you like, then
function cure_wp_amnesia_on_query_string($query_string){ if ( !is_admin() ){ if ( isset( $query_string['category_name'] ) ) { switch ($query_string['category_name']) { case 'faq': case 'corsi-di-formazione': $query_string['posts_per_page'] = 5; break; case 'prestazioni': $query_string['posts_per_page'] = 6; break; case 'smile-gallery': $query_string['posts_per_page'] = 3; //default: break; } } if ( isset( $query_string['s'] ) ){//case SEARCH $query_string['posts_per_page'] = 5; } } return $query_string; } add_filter('request', 'cure_wp_amnesia_on_query_string');
Add this function in your functions.php and set here the number of posts you want per category. I’ve not tried it with CPT but I suppose that we can find a specific index in $query_string to test against as I do with ‘category_name’ and ‘s’.
Hope this can help someone.
- The topic ‘Explanation and workaround for error 404 on category pagination’ is closed to new replies.