mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-04 22:57:20 +00:00
Query: Account for primed post caches without primed post meta/term caches.
In [54352] `update_post_caches()` was replaced by `_prime_post_caches()` to reduce excessive object cache calls. That's because `_prime_post_caches()` checks first if post IDs aren't already cached. Unfortunately this becomes an issue if a post itself is cached but not the meta/terms. To fix this regression, `_prime_post_caches()` now always calls `update_postmeta_cache()` and `update_object_term_cache()` depending on the arguments passed to it. Both functions internally check whether IDs are already cached so the fix from [54352] remains in place. Props peterwilsoncc, spacedmonkey, ocean90. Fixes #57163. git-svn-id: https://develop.svn.wordpress.org/trunk@54894 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1237,9 +1237,11 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
|
||||
|
||||
$query_1 = new WP_Query(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'fields' => $fields,
|
||||
'author' => self::$author_id,
|
||||
'post_type' => 'page',
|
||||
'fields' => $fields,
|
||||
'author' => self::$author_id,
|
||||
'update_post_meta_cache' => false,
|
||||
'update_post_term_cache' => false,
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1247,6 +1249,11 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
|
||||
$start_loop_queries = get_num_queries();
|
||||
$query_1->the_post();
|
||||
$num_loop_queries = get_num_queries() - $start_loop_queries;
|
||||
/*
|
||||
* Two expected queries:
|
||||
* 1: User meta data,
|
||||
* 2: User data.
|
||||
*/
|
||||
$this->assertSame( 2, $num_loop_queries, 'Unexpected number of queries while initializing the loop.' );
|
||||
|
||||
$start_author_queries = get_num_queries();
|
||||
@@ -1271,4 +1278,64 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
|
||||
*/
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure lazy loading term meta queries all term meta in a single query.
|
||||
*
|
||||
* @since 6.1.2
|
||||
* @ticket 57163
|
||||
* @ticket 22176
|
||||
*/
|
||||
public function test_get_post_meta_lazy_loads_all_term_meta_data() {
|
||||
$query = new WP_Query();
|
||||
|
||||
$t2 = $this->factory()->term->create(
|
||||
array(
|
||||
'taxonomy' => 'category',
|
||||
'slug' => 'bar',
|
||||
'name' => 'Bar',
|
||||
)
|
||||
);
|
||||
|
||||
wp_set_post_terms( self::$posts[0], $t2, 'category', true );
|
||||
// Clean data added to cache by factory and setting terms.
|
||||
clean_term_cache( array( self::$t1, $t2 ), 'category' );
|
||||
clean_post_cache( self::$posts[0] );
|
||||
|
||||
$num_queries_start = get_num_queries();
|
||||
$query_posts = $query->query(
|
||||
array(
|
||||
'lazy_load_term_meta' => true,
|
||||
'no_found_rows' => true,
|
||||
)
|
||||
);
|
||||
$num_queries = get_num_queries() - $num_queries_start;
|
||||
|
||||
/*
|
||||
* Four expected queries:
|
||||
* 1: Post IDs
|
||||
* 2: Post data
|
||||
* 3: Post meta data.
|
||||
* 4: Post term data.
|
||||
*/
|
||||
$this->assertSame( 4, $num_queries, 'Unexpected number of queries while querying posts.' );
|
||||
$this->assertNotEmpty( $query_posts, 'Query posts is empty.' );
|
||||
|
||||
$num_queries_start = get_num_queries();
|
||||
get_term_meta( self::$t1 );
|
||||
$num_queries = get_num_queries() - $num_queries_start;
|
||||
|
||||
/*
|
||||
* One expected query:
|
||||
* 1: Term meta data.
|
||||
*/
|
||||
$this->assertSame( 1, $num_queries, 'Unexpected number of queries during first query of term meta.' );
|
||||
|
||||
$num_queries_start = get_num_queries();
|
||||
get_term_meta( $t2 );
|
||||
$num_queries = get_num_queries() - $num_queries_start;
|
||||
|
||||
// No additional queries expected.
|
||||
$this->assertSame( 0, $num_queries, 'Unexpected number of queries during second query of term meta.' );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user