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.
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.
$paged = (get_query_var(‘page’)) ? get_query_var(‘page’) : 1;
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 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.