Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#50463 closed defect (bug) (fixed)

Don't call WP_Query::get_posts() directly

Reported by: Chouby Owned by: swissspidy
Priority: normal Milestone: 5.5
Component: Sitemaps Version: 5.5
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

WP_Query::get_posts() should not be called directly because the WP_Query constructor calls it indirectly through its call to WP_Query:query().

See: ​https://github.com/WordPress/WordPress/blob/5.4.2/wp-includes/class-wp-query.php#L3539-L3543 and ​https://github.com/WordPress/WordPress/blob/5.4.2/wp-includes/class-wp-query.php#L3432

At best calling WP_Query::get_posts() makes a useless second database query. At worst, the results are wrong as the query is not correctly initialized.

Attachments (1)

50463.diff​ (852 bytes ) - added by Chouby 6 years ago.

Download all attachments as: .zip

Change History (5)

@Chouby
6 years ago

#1 @Chouby
6 years ago

  • Keywords has-patch added

The proposed patch removes the call to WP_Query::get_posts() and replaces it by directly accessing to $query->posts.

#2 @swissspidy
6 years ago

  • Milestone Awaiting Review → 5.5

Good catch!

#3 @swissspidy
6 years ago

  • Owner set to swissspidy
  • Status new → accepted

#4 @swissspidy
6 years ago

  • Resolution → fixed
  • Status accepted → closed

In 48282:

Sitemaps: Do not unnecessarily call WP_Query::get_posts() in posts provider.

The posts have already been fetched at this point, no need to do it again.

Props Chouby.
Fixes #50463.

Note: See TracTickets for help on using tickets.