Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#19463 closed defect (bug) (wontfix)

Archive page should not return 404 when requested page is larger than max page

Reported by: kayue's profile kayue Owned by:
Milestone: Priority: normal
Severity: major Version:
Component: Query Keywords: needs-patch close
Focuses: Cc:


Right now Wordpress return 404 when requested page number is larger than max page, and this could be a problem when people changed posts_per_page on query_posts.

For example, assuming the default post per page is 10 (in Reading Settings), and there are 15 posts in archive page (total 2 pages).

In archive.php, if I change the post_per_page to 5, there will be totally 3 pages. With the current implementation, the third page will return 404 page not found, because with the default setting, this page shouldn't exist.

I suggest Wordpress should still load archive.php even requested page is larger than max page, and use have_posts() to determine what to display (similar to loop-home.php)

Related discussions in forum:

Change History (3)

#1 @dd32
12 years ago

  • Keywords close added

The correct solution here is to alter the Query BEFORE WordPress makes the query, This can be done via the pre_get_posts or the request filters, both allow you to alter the query vars which will be used for the page query.

In your example, The 3rd page of the archive IS a 404, as WordPress has already made the query, and come up with 0 posts. As a result, it's no longer an archive, as there are no posts to display in an archive. Using query_posts() within a template to change the post count is a bad thing, and something which should never be recommended - primarily for this reason, but also for performance reasons (It's effectively loading 2 pages for the single page)

This article has some decent code for changing searches posts_per_page:

function change_wp_search_size($query) {
	if ( $query->is_search ) // Make sure it is a search page
		$query->query_vars['posts_per_page'] = 10; // Change 10 to the number of posts you would like to show
	return $query; // Return our modified query variables
add_filter('pre_get_posts', 'change_wp_search_size'); // Hook our custom function onto the request filter

replace ->is_search with ->is_archive and/or other query-related alterations you'd like to make (ie. only category archives, or tag ID 345, etc) and WordPress will work "as expected"

#3 @scribu
12 years ago

  • Component changed from Rewrite Rules to Query
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

What dd32 said.

Note: See TracTickets for help on using tickets.