diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 3bfede95da..97a3fb8f77 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -7489,6 +7489,22 @@ function update_post_author_caches( $posts ) { cache_users( $author_ids ); } +/** + * Prime post parent post caches. + * + * @since 6.1.0 + * + * @param WP_Post[] $posts Array of Post objects. + */ +function update_post_parent_caches( $posts ) { + $parent_ids = wp_list_pluck( $posts, 'post_parent' ); + $parent_ids = array_map( 'absint', $parent_ids ); + $parent_ids = array_unique( array_filter( $parent_ids ) ); + if ( ! empty( $parent_ids ) ) { + _prime_post_caches( $parent_ids, false ); + } +} + /** * Updates metadata cache for a 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 ae203b41aa..87de7a2421 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,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { $posts = array(); + update_post_parent_caches( $query_result ); update_post_author_caches( $query_result ); if ( post_type_supports( $this->post_type, 'thumbnail' ) ) { update_post_thumbnail_cache( $posts_query ); diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index e4d7b5de4b..54d1cc0167 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -1787,6 +1787,47 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertSame( $formats, $data['schema']['properties']['format']['enum'] ); } + /** + * @ticket 55593 + */ + public function test_get_items_parent_ids_primed() { + $parent_id1 = self::$post_ids[0]; + $parent_id2 = self::$post_ids[1]; + $parent_ids = array( $parent_id2, $parent_id1 ); + + $this->factory->attachment->create_object( + DIR_TESTDATA . '/images/canola.jpg', + $parent_id1, + array( + 'post_mime_type' => 'image/jpeg', + 'post_excerpt' => 'A sample caption 1', + ) + ); + + $this->factory->attachment->create_object( + DIR_TESTDATA . '/images/canola.jpg', + $parent_id2, + array( + 'post_mime_type' => 'image/jpeg', + 'post_excerpt' => 'A sample caption 2', + ) + ); + + // Attachment creation warms parent ids. Needs clean up for test. + wp_cache_delete_multiple( $parent_ids, 'posts' ); + + $filter = new MockAction(); + add_filter( 'update_post_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); + rest_get_server()->dispatch( $request ); + + $args = $filter->get_args(); + $last = end( $args ); + $this->assertIsArray( $last, 'The last value is not an array' ); + $this->assertEqualSets( $parent_ids, $last[1] ); + } + public function test_get_item() { $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = rest_get_server()->dispatch( $request );