Make WordPress Core

Opened 10 years ago

Last modified 2 weeks ago

#39428 new defect (bug)

Die fast in get_posts if get_user_by returns false

Reported by: david.binda Owned by:
Priority: normal Milestone: Future Release
Component: Query Version: 4.7
Severity: normal Keywords: has-patch changes-requested has-unit-tests
Cc: Focuses: performance

Description

In case a username in the URL or passed to WP_Query via any other way is invalid - such a user is not in database - the code currently casts the false value returned from get_user_by to 0 via absint - see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/class-wp-query.php#L2126

Instead of trying to query unassigned posts (with post_author equal to 0) for all invalid usernames, the query should die fast and return no posts.

Attachments (2)

39428.diff​ (1.6 KB ) - added by david.binda 10 years ago.
39428.2.diff​ (499 bytes ) - added by SirLouen 17 months ago.

Download all attachments as: .zip

Change History (6)

#1 @swissspidy
10 years ago

  • Keywords has-patch has-unit-tests added

#2 @desrosj
8 years ago

  • Keywords needs-testing added
  • Milestone Awaiting Review → Future Release

#3 in reply to: ↑ description @SirLouen
17 months ago

  • Focuses performance added
  • Keywords changes-requested added; needs-testing removed

Replying to david.binda:

Instead of trying to query unassigned posts (with post_author equal to 0) for all invalid usernames, the query should die fast and return no posts.

Why not short-circuiting fast and furiously?
Unit tests are too focused on that strange SQL condition of 1==0. I think can be simpler, just ignoring those two assertNotRegExp and good to go.

I've added a patch with this.

If you like, you can integrate changes in a GitHub PR (both short-circuit and changes in Unit-Tests), and we could try to move this forward. It's a very niche case, but it's true that we save at least, one query to the DB. It's a little performance gain.

This ticket was mentioned in ​PR #15350 on ​WordPress/wordpress-develop by ​@jdeep.


2 weeks ago
#4

When get_user_by( 'slug' ) finds no user, WP_Query adds post_author = 0 to the WHERE clause. So /author/no-such-user/ returns 200 and lists any posts that have no author.

This patch records that clause. After posts_pre_query, if the clause is still in the SQL, WP_Query returns no posts and skips the database query.

Why not return early (like 39428.2.diff)

Co-Authors Plus guest authors have no wp_users row. The plugin finds the post_author = 0 clause and replaces it in posts_where. Returning early breaks guest author archives. This patch only skips the query when nothing has replaced the clause.

Use of AI Tools

Note: See TracTickets for help on using tickets.