WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#14324 closed defect (bug) (invalid)

404 when querying custom post types on author.php

Reported by: truthmedia Owned by:
Milestone: Priority: normal
Severity: major Version: 3.0
Component: Posts, Post Types Keywords: post_type, author.php, 404, custom post types
Focuses: Cc:

Description

Not sure if it's the only place, but on the author.php template, when you change the post_type of the loop, WordPress seems to only want to return the number of pages it WOULD have had if post_type=post was used.

Example: On our author.php template, we've changed the loop to use the following custom query:
author=123&post_type=any&post_status=publish&orderby=title&order=ASC&paged=6

In this case, it's looking for all published pages and posts (any) by the author with the ID of 123, and we're looking at page six of the results. On our site, this page shows up fine, but when we go to page 7 using the standard wordpress next/prev links we get a 404 error, in spite of the fact that there are MANY more pages and posts to be displayed.

If I configure the code to only show posts, it shows 6 pages of results which is exactly how many it SHOULD show.

Therefore, it seems that there is something in the code that detects whether or not the page is a 404, which is predisposed to only showing posts as opposed to "any".

Change History (2)

comment:1 truthmedia4 years ago

On further analysis, the problem is a logical one, rather than a code based one. WordPress initially determines what sort of page to display (ie author or 404) based on the query obtained by the URL. Because this initial query has no post_type specified, it defaults to "post". As such, the 404 is sent long before the code would even have a chance to reach our custom query. I can probably devise a workaround, however this is still a business logic problem when custom queries are able to return more posts than the original page might have.

comment:2 scribu4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

404s are sent before the template file is chosen, which just makes sense.

So, you should use the 'pre_get_posts' action to alter your query (the code should be placed in functions.php).

Note: See TracTickets for help on using tickets.