[wp-trac] [WordPress Trac] #53280: Incorrect use of WP_Query::get_posts in new get_block_template and get_block_templates functions

WordPress Trac noreply at wordpress.org
Wed May 26 10:30:19 UTC 2021


#53280: Incorrect use of WP_Query::get_posts in new get_block_template and
get_block_templates functions
--------------------------+-----------------------------
 Reporter:  david.binda   |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Widgets       |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:  performance   |
--------------------------+-----------------------------
 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

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53280>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list