This is an annoying bug that has plagued WordPress for as long as I can remember, it’s rare that I run into it because I rarely need to have individual parts of a site control the number of posts I need to display with paging.
The Problem:
I have a custom post type called “project” as well as a blog section which just uses the default “post” post type. For projects I enabled archive support and then created a custom archive file called archive-project.php. I want to limit the number of projects being displayed per page to just 1 without changing the value in the WordPress reading options screen because I need that for the blog section, so naturally you’d expect to be able to do the following before the loop.
[code]
global $query_string;
$paged = (get_query_var(‘page’)) ? get_query_var(‘page’) : 1;
query_posts($query_string.“paged=$paged&posts_per_page=1”);
[/code]
The above code snippet does indeed work for the first page, but visiting any subsequent pages like page 2 or page 3 will generate a 404 error. Although we’ve told WordPress we only want one project to show per page of our archive, once we hit the second page it thinks we’re using the options page value which by default is set to 10. The posts_per_page value is overwriting our value, this is an issue that many seem to encounter and most solutions don’t work or are heavily overcomplicated.
The quick fix is to set the value in the reading options section of the WordPress admin panel to be the number we want and then implicitly tell it to show more on parts of the site where we need more than one post shown at once like our blog section which we want to show 10 posts, not one.
The Fix:
The following code solution fixes the problem. Simply insert it at the bottom of your functions.php file and enter your post type name and required number to display. The code is simple enough to be tweaked for other purposes as well and if I can help, leave a comment and we’ll see what I can do.
/**
* Wordpress has a known bug with the posts_per_page value and overriding it using
* query_posts. The result is that although the number of allowed posts_per_page
* is abided by on the first page, subsequent pages give a 404 error and act as if
* there are no more custom post type posts to show and thus gives a 404 error.
*
* This fix is a nicer alternative to setting the blog pages show at most value in the
* WP Admin reading options screen to a low value like 1.
*
*/
function custom_posts_per_page( $query ) {
if ( $query->is_archive('project') ) {
set_query_var('posts_per_page', 1);
}
}
add_action( 'pre_get_posts', 'custom_posts_per_page' );