diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index e290bbf27b..f555b1bb18 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -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. diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index a027c35f23..d95f26d20d 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -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; diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 4d1c8955a6..fb69b71ee5 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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. * diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index dfb97f7b60..7b6072f2ec 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -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; diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 9a8a9b3ffd..67a4854be3 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -620,6 +620,39 @@ class Tests_Query extends WP_UnitTestCase { $this->assertSame( array( $p2 ), $q->posts ); } + /** + * @ticket 55716 + */ + public function test_prime_user_cache() { + $action = new MockAction(); + add_filter( 'update_user_metadata_cache', array( $action, 'filter' ), 10, 2 ); + $user_ids = array(); + $count = 5; + for ( $i = 0; $i < $count; $i ++ ) { + $user_ids[ $i ] = self::factory()->user->create(); + self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_author' => $user_ids[ $i ], + ) + ); + } + + $q = new WP_Query( + array( + 'post_type' => 'post', + 'posts_per_page' => $count, + ) + ); + while ( $q->have_posts() ) { + $q->the_post(); + } + + $args = $action->get_args(); + $last_args = end( $args ); + $this->assertSameSets( $user_ids, $last_args[1], 'Ensure that user ids are primed' ); + } + /** * @ticket 35601 */