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.

Built from https://develop.svn.wordpress.org/trunk@53482


git-svn-id: http://core.svn.wordpress.org/trunk@53071 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2022-06-10 13:39:11 +00:00
parent 558cd4c353
commit 2237f67458
5 changed files with 22 additions and 5 deletions

View File

@ -3434,6 +3434,9 @@ class WP_Query {
*/
public function the_post() {
global $post;
if ( ! $this->in_the_loop ) {
update_post_author_caches( $this->posts );
}
$this->in_the_loop = true;
if ( -1 == $this->current_post ) { // Loop has just started.

View File

@ -132,6 +132,8 @@ if ( ! function_exists( 'cache_users' ) ) :
function cache_users( $user_ids ) {
global $wpdb;
update_meta_cache( 'user', $user_ids );
$clean = _get_non_cached_ids( $user_ids, 'users' );
if ( empty( $clean ) ) {
@ -141,13 +143,9 @@ if ( ! function_exists( 'cache_users' ) ) :
$list = implode( ',', $clean );
$users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
$ids = array();
foreach ( $users as $user ) {
update_user_caches( $user );
$ids[] = $user->ID;
}
update_meta_cache( 'user', $ids );
}
endif;

View File

@ -7474,6 +7474,20 @@ function update_post_caches( &$posts, $post_type = 'post', $update_term_cache =
}
}
/**
* Prime post author user caches.
*
* @since 6.1.0
*
* @param WP_Post[] $posts Array of Post objects
*/
function update_post_author_caches( $posts ) {
$author_ids = wp_list_pluck( $posts, 'post_author' );
$author_ids = array_map( 'absint', $author_ids );
$author_ids = array_unique( array_filter( $author_ids ) );
cache_users( $author_ids );
}
/**
* Updates metadata cache for list of post IDs.
*

View File

@ -369,6 +369,8 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$posts = array();
update_post_author_caches( $query_result );
foreach ( $query_result as $post ) {
if ( ! $this->check_read_permission( $post ) ) {
continue;

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-53481';
$wp_version = '6.1-alpha-53482';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.