Query: Prevent ID only queries erroring when starting the loop.

Ensure only full post objects are passed to `update_post_author_caches()` when called within `WP_Query::the_post()`. This prevents an error when starting the Loop for Queries initiated with a subset of fields or IDs only.

Props konyoldeath, dd32, lozula, TimothyBlynJacobs, spacedmonkey, mxbclang, peterwilsoncc.
Fixes #56948.



git-svn-id: https://develop.svn.wordpress.org/trunk@54771 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
peterwilsoncc
2022-11-09 00:57:02 +00:00
parent 6903cfd822
commit 4edede48eb
3 changed files with 79 additions and 1 deletions

View File

@@ -1210,4 +1210,65 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
array( 'id=>parent', 'id=>parent' ),
);
}
/**
* Ensure starting the loop warms the author cache.
*
* @since 6.1.1
* @ticket 56948
*
* @covers WP_Query::the_post
*
* @dataProvider data_author_cache_warmed_by_the_loop
*
* @param string $fields Query fields.
*/
public function test_author_cache_warmed_by_the_loop( $fields ) {
// Update post author for the parent post.
self::factory()->post->update_object( self::$pages[0], array( 'post_author' => self::$author_id ) );
self::factory()->post->create(
array(
'post_author' => self::$author_id,
'post_parent' => self::$pages[0],
'post_type' => 'page',
)
);
$query_1 = new WP_Query(
array(
'post_type' => 'page',
'fields' => $fields,
'author' => self::$author_id,
)
);
// Start the loop.
$start_loop_queries = get_num_queries();
$query_1->the_post();
$num_loop_queries = get_num_queries() - $start_loop_queries;
$this->assertSame( 2, $num_loop_queries, 'Unexpected number of queries while initializing the loop.' );
$start_author_queries = get_num_queries();
get_user_by( 'ID', self::$author_id );
$num_author_queries = get_num_queries() - $start_author_queries;
$this->assertSame( 0, $num_author_queries, 'Author cache is not warmed by the loop.' );
}
/**
* Data provider for test_author_cache_warmed_by_the_loop
*
* @return array[]
*/
public function data_author_cache_warmed_by_the_loop() {
return array(
'fields: empty' => array( '' ),
'fields: all' => array( 'all' ),
'fields: ids' => array( 'ids' ),
/*
* `id=>parent` is untested pending the resolution of an existing bug.
* See https://core.trac.wordpress.org/ticket/56992
*/
);
}
}