WordPress.org

Make WordPress Core

Opened 4 months ago

Closed 4 months ago

#53280 closed defect (bug) (fixed)

Incorrect use of WP_Query::get_posts in new get_block_template and get_block_templates functions

Reported by: david.binda Owned by: jorbin
Milestone: 5.8 Priority: normal
Severity: normal Version: 5.8
Component: Widgets Keywords: has-patch
Focuses: performance Cc:

Description

The usage of WP_Query in both get_block_template and get_block_templates does not feel right to me.

Both functions create a new WP_Query object while passing in the args for the query, and are then calling the WP_Query::get_posts, which leads to a duplicate SQL query execution.

When passing args to WP_Query::__construct method, this one internally executes the WP_Query::get_posts method and stores the result in the WP_Query::posts property. When calling the WP_Query::get_posts again, the same SQL query gets executed, and the result is again stored in the WP_Query::posts property.

Thus, the correct usage, IMHO, is to read the posts already stored in the WP_Query::posts property.

See https://build.trac.wordpress.org/browser/trunk/wp-includes/block-template-utils.php?rev=50612#L91 as well as https://build.trac.wordpress.org/browser/trunk/wp-includes/block-template-utils.php?rev=50612#L133

Both cases were introduced in r50612

Attachments (2)

53280.diff (830 bytes) - added by david.binda 4 months ago.
53280.2.diff (741 bytes) - added by david.binda 4 months ago.

Download all attachments as: .zip

Change History (8)

@david.binda
4 months ago

#1 @david.binda
4 months ago

There seems to be the same issue in the wp_filter_wp_template_unique_post_slug function (also part of the same commit - r51003 ). See https://core.trac.wordpress.org/browser/trunk/src/wp-includes/theme-templates.php?rev=51003#L52 and https://core.trac.wordpress.org/browser/trunk/src/wp-includes/theme-templates.php?rev=51003#L62

I'm attaching another patch, addressing the other bit. Not sure if I should combine those patches, or if a new ticket should be opened for the other instance.

@david.binda
4 months ago

#2 @desrosj
4 months ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 5.8

#3 @desrosj
4 months ago

We're at the beta 1 deadline, but this should remain and be evaluated during beta since it directly relates to new functionality.

This ticket was mentioned in Slack in #core by chaion07. View the logs.


4 months ago

#5 @jorbin
4 months ago

  • Owner set to jorbin
  • Status changed from new to assigned

#6 @jorbin
4 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 51144:

Block Editor: Prevent duplicate queries

When passing args to WP_Query::__construct method (in this case, but creating a new WP_Query, this one internally executes the WP_Query::get_posts method and stores the result in the WP_Query::posts property. When calling the WP_Query::get_posts again, the same SQL query gets executed, and the result is again stored in the WP_Query::posts property.

This was introduced in [51003].

Props david.binda, jorbin.
Fixes #53280. See #53176.

Note: See TracTickets for help on using tickets.