Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#50463 closed defect (bug) (fixed)

Don't call WP_Query::get_posts() directly

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

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 4 years ago.

Download all attachments as: .zip

Change History (5)

@Chouby
4 years ago

#1 @Chouby
4 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
4 years ago

  • Milestone changed from Awaiting Review to 5.5

Good catch!

#3 @swissspidy
4 years ago

  • Owner set to swissspidy
  • Status changed from new to accepted

#4 @swissspidy
4 years ago

  • Resolution set to fixed
  • Status changed from accepted to 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.