From b98b347504a572a7685ad627f79df4e8e8903578 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 16 Mar 2025 22:55:11 +0000 Subject: Query: Fix performance regression starting the loop for `all` fields. Fixes a performance regression starting the loop after calling `WP_Query( [ 'fields' => 'all' ] )`. This changes how `WP_Query::the_post()` determines whether there is a need to traverse the posts for cache warming. If IDs are queried, `WP_Query::$posts` is assumed to be an array of post IDs. If all fields are queried, `WP_Query::$posts` is assumed to be an array of fully populated post objects. Follow up to [59919], [59937]. Props joemcgill, peterwilsoncc, SirLouen. Fixes #56992. git-svn-id: https://develop.svn.wordpress.org/trunk@59993 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/query/thePost.php | 84 ++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/phpunit/tests/query/thePost.php b/tests/phpunit/tests/query/thePost.php index e99a517872..6032a01dbe 100644 --- a/tests/phpunit/tests/query/thePost.php +++ b/tests/phpunit/tests/query/thePost.php @@ -48,6 +48,86 @@ class Tests_Query_ThePost extends WP_UnitTestCase { } } + /** + * Ensure custom 'fields' values are respected. + * + * @ticket 56992 + */ + public function test_wp_query_respects_custom_fields_values() { + global $wpdb; + add_filter( + 'posts_fields', + function ( $fields, $query ) { + global $wpdb; + + if ( $query->get( 'fields' ) === 'custom' ) { + $fields = "$wpdb->posts.ID,$wpdb->posts.post_author"; + } + + return $fields; + }, + 10, + 2 + ); + + $query = new WP_Query( + array( + 'fields' => 'custom', + 'post_type' => 'page', + 'post__in' => self::$page_child_ids, + ) + ); + + $this->assertNotEmpty( $query->posts, 'The query is expected to return results' ); + $this->assertSame( $query->get( 'fields' ), 'custom', 'The WP_Query class is expected to use the custom fields value' ); + $this->assertStringContainsString( "$wpdb->posts.ID,$wpdb->posts.post_author", $query->request, 'The database query is expected to use the custom fields value' ); + } + + /** + * Ensure custom 'fields' populates the global post in the loop. + * + * @ticket 56992 + */ + public function test_wp_query_with_custom_fields_value_populates_the_global_post() { + global $wpdb; + add_filter( + 'posts_fields', + function ( $fields, $query ) { + global $wpdb; + + if ( $query->get( 'fields' ) === 'custom' ) { + $fields = "$wpdb->posts.ID,$wpdb->posts.post_author"; + } + + return $fields; + }, + 10, + 2 + ); + + $query = new WP_Query( + array( + 'fields' => 'custom', + 'post_type' => 'page', + 'post__in' => self::$page_child_ids, + 'orderby' => 'id', + 'order' => 'ASC', + ) + ); + + $query->the_post(); + + // Get the global post and specific post. + $global_post = get_post(); + $specific_post = get_post( self::$page_child_ids[0], ARRAY_A ); + + $this->assertSameSetsWithIndex( $specific_post, $global_post->to_array(), 'The global post is expected to be fully populated.' ); + + $this->assertNotEmpty( get_the_title(), 'The title is expected to be populated.' ); + $this->assertNotEmpty( get_the_content(), 'The content is expected to be populated.' ); + $this->assertNotEmpty( get_the_excerpt(), 'The excerpt is expected to be populated.' ); + } + /** * Ensure that a secondary loop populates the global post completely regardless of the fields parameter. * @@ -75,11 +155,11 @@ class Tests_Query_ThePost extends WP_UnitTestCase { $global_post = get_post(); $specific_post = get_post( self::$page_child_ids[0], ARRAY_A ); + $this->assertSameSetsWithIndex( $specific_post, $global_post->to_array(), 'The global post is expected to be fully populated.' ); + $this->assertNotEmpty( get_the_title(), 'The title is expected to be populated.' ); $this->assertNotEmpty( get_the_content(), 'The content is expected to be populated.' ); $this->assertNotEmpty( get_the_excerpt(), 'The excerpt is expected to be populated.' ); - - $this->assertSameSetsWithIndex( $specific_post, $global_post->to_array(), 'The global post is expected to be fully populated.' ); } /** -- cgit v1.2.3