diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index f555b1bb18..efa7dc8db3 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3434,9 +3434,11 @@ 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 d95f26d20d..bd80946c0c 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -143,6 +143,7 @@ if ( ! function_exists( 'cache_users' ) ) : $list = implode( ',', $clean ); $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" ); + foreach ( $users as $user ) { update_user_caches( $user ); } diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index fb69b71ee5..9b3b4bc28c 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -7424,11 +7424,11 @@ function clean_post_cache( $post ) { } /** - * Calls major cache updating functions for list of Post objects. + * Updates post, term, and metadata caches for a list of post objects. * * @since 1.5.0 * - * @param WP_Post[] $posts Array of Post objects + * @param WP_Post[] $posts Array of post objects (passed by reference). * @param string $post_type Optional. Post type. Default 'post'. * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. @@ -7475,21 +7475,22 @@ function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = } /** - * Prime post author user caches. + * Updates post author user caches for a list of post objects. * * @since 6.1.0 * - * @param WP_Post[] $posts Array of Post objects + * @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. + * Updates metadata cache for a list of post IDs. * * Performs SQL query to retrieve the metadata for the post IDs and updates the * metadata cache for the posts. Therefore, the functions, which call this diff --git a/tests/phpunit/tests/post/updatePostAuthorCaches.php b/tests/phpunit/tests/post/updatePostAuthorCaches.php new file mode 100644 index 0000000000..0c5823561b --- /dev/null +++ b/tests/phpunit/tests/post/updatePostAuthorCaches.php @@ -0,0 +1,75 @@ +user->create(); + $factory->post->create( + array( + 'post_type' => 'post', + 'post_author' => self::$user_ids[ $i ], + ) + ); + } + } + + /** + * @ticket 55716 + */ + public function test_update_post_author_caches() { + $action = new MockAction(); + add_filter( 'update_user_metadata_cache', array( $action, 'filter' ), 10, 2 ); + + $q = new WP_Query( + array( + 'post_type' => 'post', + 'posts_per_page' => self::$post_author_count, + ) + ); + + while ( $q->have_posts() ) { + $q->the_post(); + } + + $args = $action->get_args(); + $last_args = end( $args ); + + $this->assertSameSets( self::$user_ids, $last_args[1], 'Ensure that user IDs are primed' ); + } +} diff --git a/tests/phpunit/tests/post/updatePostCache.php b/tests/phpunit/tests/post/updatePostCache.php index 976f7af118..cfb279b67c 100644 --- a/tests/phpunit/tests/post/updatePostCache.php +++ b/tests/phpunit/tests/post/updatePostCache.php @@ -1,12 +1,12 @@ 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 */