1

Inspecting the rewrite rules for author archives I can see that any digit used after page/ is matched.

author/([^/]+)/page/?([0-9]{1,})/?$
=>
index.php?author_name=$matches[1]&paged=$matches[2]

In theory, this means the following pages are possible

  • author/username/page/262/
  • author/username/page/26278/
  • author/username/page/26278292/

In real life if you visit these pages you'll get a 404 (unless the author has been crazy busy posting).

How does WordPress determine if the value of the paged query var is too high and in such cases set the 404?

1
  • I just found WP::handle_404() :) Commented May 28, 2017 at 0:01

1 Answer 1

2

A query either returns posts or it doesn't. Whether it's a non-existent page number, a non-existent category name, a non-existent post name, etc., it's all the same.

The rewrite rules is matched, which populates the query vars, which forms the query. The query is sent to the database, and the database returns some amount of posts between zero and whatever number was requested.

The only difference between all of those cases is what it does after it's determined to be a 404, which depends on what rewrite rule was matched. In the case that the name query var is populated, it tries to find the closest match with an additional LIKE query, and redirects there if something is found.

16
  • Thanks for the info. So in general 1) a 404 is set when a query doesn't return posts and 2) a 404 isn't set when a query returns posts. In the second case, should a 404 be set when the URL is something like example.com/page-slug/page/3716151/ and there's no posts to show? Commented May 28, 2017 at 11:46
  • No, because there is a post to show- whatever post page-slug represents.
    – Milo
    Commented May 28, 2017 at 12:09
  • I'm wondering then what would be displayed on example.com/page-slug/page/3716151/? Surely we wouldn't want the same content as example.com/page-slug/? Commented May 28, 2017 at 12:16
  • It's probably a difficult concept to grasp if you're not familiar with SQL. There is no such thing as pages to the db engine, it's a calculation that happens in PHP when the query is formed. /page/n/ is the pattern for archive pagination, the effect it would have on the query doesnt make sense when you are requesting a specific post. Singular post pagination is a different query var (page vs paged) and that pagination happens in PHP after the whole post is returned from the database. I'm probably not explaining this very well...
    – Milo
    Commented May 28, 2017 at 12:26
  • Check out WP::handle_404(). I think this runs after the query is run and is where 404s are set. Commented May 28, 2017 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.