summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorPeter Wilson <peterwilsoncc@git.wordpress.org>2025-03-16 22:55:11 +0000
committerPeter Wilson <peterwilsoncc@git.wordpress.org>2025-03-16 22:55:11 +0000
commitb98b347504a572a7685ad627f79df4e8e8903578 (patch)
tree4a07b447b94105d82e4c53af9437989b8e335c13 /tests
parent7950bbed138f27bc055b6f688fc6962df1945cf5 (diff)
downloadwordpress-b98b347504a572a7685ad627f79df4e8e8903578.tar.gz
wordpress-b98b347504a572a7685ad627f79df4e8e8903578.zip
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
Diffstat (limited to 'tests')
-rw-r--r--tests/phpunit/tests/query/thePost.php84
1 files changed, 82 insertions, 2 deletions
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
@@ -49,6 +49,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.
*
* @ticket 56992
@@ -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.' );
}
/**