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: |
|
Owned by: |
|
---|---|---|---|
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
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#L62I'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.