diff --git a/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php b/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php index 0e94f57bda..03f8ff295e 100644 --- a/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php +++ b/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php @@ -58,12 +58,11 @@ class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler { } $query_args = array( - 'post_type' => $post_types, - 'post_status' => 'publish', - 'paged' => (int) $request['page'], - 'posts_per_page' => (int) $request['per_page'], - 'ignore_sticky_posts' => true, - 'fields' => 'ids', + 'post_type' => $post_types, + 'post_status' => 'publish', + 'paged' => (int) $request['page'], + 'posts_per_page' => (int) $request['per_page'], + 'ignore_sticky_posts' => true, ); if ( ! empty( $request['search'] ) ) { @@ -83,7 +82,9 @@ class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler { $query_args = apply_filters( 'rest_post_search_query', $query_args, $request ); $query = new WP_Query(); - $found_ids = $query->query( $query_args ); + $posts = $query->query( $query_args ); + // Querying the whole post object will warm the object cache, avoiding an extra query per result. + $found_ids = wp_list_pluck( $posts, 'ID' ); $total = $query->found_posts; return array( diff --git a/tests/phpunit/tests/rest-api/rest-search-controller.php b/tests/phpunit/tests/rest-api/rest-search-controller.php index f6706b18ff..afe0d75758 100644 --- a/tests/phpunit/tests/rest-api/rest-search-controller.php +++ b/tests/phpunit/tests/rest-api/rest-search-controller.php @@ -332,6 +332,36 @@ class WP_Test_REST_Search_Controller extends WP_Test_REST_Controller_Testcase { ); } + /** + * @ticket 55674 + */ + public function test_get_items_search_prime_ids() { + $action = new MockAction(); + add_filter( 'query', array( $action, 'filter' ), 10, 2 ); + + $query_args = array( + 'per_page' => 100, + 'search' => 'foocontent', + ); + $response = $this->do_request_with_params( $query_args ); + $this->assertSame( 200, $response->get_status(), 'Request Status Response is not 200.' ); + + $ids = wp_list_pluck( $response->get_data(), 'id' ); + $this->assertSameSets( self::$my_content_post_ids, $ids, 'Query result posts ids do not match with expected ones.' ); + + $args = $action->get_args(); + $primed_query_found = false; + foreach ( $args as $arg ) { + // Primed query will use WHERE ID IN clause. + if ( str_contains( $arg[0], 'WHERE ID IN (' . implode( ',', $ids ) ) ) { + $primed_query_found = true; + break; + } + } + + $this->assertTrue( $primed_query_found, 'Prime query was not executed.' ); + } + /** * Test retrieving a single item isn't possible. */