Make WordPress Core


Ignore:
Timestamp:
06/10/2022 01:37:52 PM (2 years ago)
Author:
spacedmonkey
Message:

Prime users cache in WP_Query and post REST API controller.

For a call to WP_Query or a post REST API request that contains posts from multiple authors, call the cache_users function, to ensure that all user data for post authors is primed in
a single database query. This results in far fewer database queries on multiple author sites.

Props spacedmonkey, timothyblynjacobs, peterwilsoncc.
Fixes #55716.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r53456 r53482  
    74767476
    74777477/**
     7478 * Prime post author user caches.
     7479 *
     7480 * @since 6.1.0
     7481 *
     7482 * @param WP_Post[] $posts Array of Post objects
     7483 */
     7484function update_post_author_caches( $posts ) {
     7485    $author_ids = wp_list_pluck( $posts, 'post_author' );
     7486    $author_ids = array_map( 'absint', $author_ids );
     7487    $author_ids = array_unique( array_filter( $author_ids ) );
     7488    cache_users( $author_ids );
     7489}
     7490
     7491/**
    74787492 * Updates metadata cache for list of post IDs.
    74797493 *
Note: See TracChangeset for help on using the changeset viewer.