<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[54892] branches/6.1: Query: Account for primed post caches without primed post meta/term caches.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/54892">54892</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/54892","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-11-29 20:09:24 +0000 (Tue, 29 Nov 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Query: Account for primed post caches without primed post meta/term caches.

In <a href="https://core.trac.wordpress.org/changeset/54352">[54352]</a> `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 <a href="https://core.trac.wordpress.org/changeset/54352">[54352]</a> remains in place.

Props peterwilsoncc, spacedmonkey, ocean90.
Fixes <a href="https://core.trac.wordpress.org/ticket/57163">#57163</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches61srcwpincludespostphp">branches/6.1/src/wp-includes/post.php</a></li>
<li><a href="#branches61testsphpunittestsquerycacheResultsphp">branches/6.1/tests/phpunit/tests/query/cacheResults.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#branches61testsphpunittestspostprimePostCachesphp">branches/6.1/tests/phpunit/tests/post/primePostCaches.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches61srcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.1/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.1/src/wp-includes/post.php     2022-11-29 15:49:49 UTC (rev 54891)
+++ branches/6.1/src/wp-includes/post.php       2022-11-29 20:09:24 UTC (rev 54892)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7822,6 +7822,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_queue_posts_for_term_meta_lazyload( $posts ) {
</span><span class="cx" style="display: block; padding: 0 10px">        $post_type_taxonomies = array();
</span><span class="cx" style="display: block; padding: 0 10px">        $term_ids             = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $prime_post_terms     = array();
</ins><span class="cx" style="display: block; padding: 0 10px">         foreach ( $posts as $post ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! ( $post instanceof WP_Post ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        continue;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7832,6 +7833,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $prime_post_terms[ $taxonomy ][] = $post->ID;
+               }
+       }
+
+       foreach ( $prime_post_terms as $taxonomy => $post_ids ){
+               wp_cache_get_multiple( $post_ids, "{$taxonomy}_relationships" );
+       }
+
+       foreach ( $posts as $post ) {
+               foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         // Term cache should already be primed by `update_post_term_cache()`.
</span><span class="cx" style="display: block; padding: 0 10px">                        $terms = get_object_term_cache( $post->ID, $taxonomy );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( false !== $terms ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7877,7 +7888,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.4.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 6.1.0 This function is no longer marked as "private".
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @see update_post_caches()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @see update_post_cache()
+ * @see update_postmeta_cache()
+ * @see update_object_term_cache()
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7892,8 +7905,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! empty( $non_cached_ids ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $fresh_posts ) {
+                       // Despite the name, update_post_cache() expects an array rather than a single post.
+                       update_post_cache( $fresh_posts );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       if ( $update_meta_cache ) {
+               update_postmeta_cache( $ids );
+       }
+
+       if ( $update_term_cache ) {
+               $post_types = array_map( 'get_post_type', $ids );
+               $post_types = array_unique( $post_types );
+               update_object_term_cache( $ids, $post_types );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="branches61testsphpunittestspostprimePostCachesphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/6.1/tests/phpunit/tests/post/primePostCaches.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.1/tests/phpunit/tests/post/primePostCaches.php                         (rev 0)
+++ branches/6.1/tests/phpunit/tests/post/primePostCaches.php   2022-11-29 20:09:24 UTC (rev 54892)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,229 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test `_prime_post_caches()`.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Test class for `_prime_post_caches()`.
+ *
+ * @group post
+ * @group cache
+ *
+ * @covers ::_prime_post_caches
+ */
+class Tests_Post_PrimePostCaches extends WP_UnitTestCase {
+
+       /**
+        * Post IDs.
+        *
+        * @var int[]
+        */
+       public static $posts;
+
+       /**
+        * Set up test resources before the class.
+        *
+        * @param WP_UnitTest_Factory $factory The unit test factory.
+        */
+       public static function wpSetupBeforeClass( WP_UnitTest_Factory $factory ) {
+               self::$posts = $factory->post->create_many( 3 );
+
+               $category = $factory->term->create(
+                       array(
+                               'taxonomy' => 'category',
+                               'slug'     => 'foo',
+                               'name'     => 'Foo',
+                       )
+               );
+
+               wp_set_post_terms( self::$posts[0], $category, 'category' );
+               add_post_meta( self::$posts[0], 'meta', 'foo' );
+               add_post_meta( self::$posts[1], 'meta', 'bar' );
+       }
+
+       /**
+        * @ticket 57163
+        */
+       public function test_prime_post_caches() {
+               $post_id = self::$posts[0];
+
+               $this->assertSame( array( $post_id ), _get_non_cached_ids( array( $post_id ), 'posts' ), 'Post is already cached.' );
+
+               // Test posts cache.
+               $before_num_queries = get_num_queries();
+               _prime_post_caches( array( $post_id ) );
+               $num_queries = get_num_queries() - $before_num_queries;
+
+               /*
+                * Four expected queries:
+                * 1: Posts data,
+                * 2: Post meta data,
+                * 3: Taxonomy data,
+                * 4: Term data.
+                */
+               $this->assertSame( 4, $num_queries, 'Unexpected number of queries.' );
+
+               $this->assertSame( array(), _get_non_cached_ids( array( $post_id ), 'posts' ), 'Post is not cached.' );
+
+               // Test post meta cache.
+               $before_num_queries = get_num_queries();
+               $meta               = get_post_meta( $post_id, 'meta', true );
+               $num_queries        = get_num_queries() - $before_num_queries;
+
+               $this->assertSame( 'foo', $meta, 'Meta has unexpected value.' );
+               $this->assertSame( 0, $num_queries, 'Unexpected number of queries.' );
+
+               // Test term cache.
+               $before_num_queries = get_num_queries();
+               $categories         = get_the_category( $post_id );
+               $num_queries        = get_num_queries() - $before_num_queries;
+
+               $this->assertNotEmpty( $categories, 'Categories does return an empty result set.' );
+               $this->assertSame( 0, $num_queries, 'Unexpected number of queries.' );
+       }
+
+       /**
+        * @ticket 57163
+        */
+       public function test_prime_post_caches_with_multiple_posts() {
+               $this->assertSame( self::$posts, _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are already cached.' );
+
+               $before_num_queries = get_num_queries();
+               _prime_post_caches( self::$posts );
+               $num_queries = get_num_queries() - $before_num_queries;
+
+               /*
+                * Four expected queries:
+                * 1: Posts data,
+                * 2: Post meta data,
+                * 3: Taxonomy data,
+                * 4: Term data.
+                */
+               $this->assertSame( 4, $num_queries, 'Unexpected number of queries.' );
+
+               $this->assertSame( array(), _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are not cached.' );
+       }
+
+       /**
+        * @ticket 57163
+        */
+       public function test_prime_post_caches_only_posts_cache() {
+               $this->assertSame( self::$posts, _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are already cached.' );
+
+               $before_num_queries = get_num_queries();
+               _prime_post_caches( self::$posts, false, false );
+               $num_queries = get_num_queries() - $before_num_queries;
+
+               /*
+                * One expected query:
+                * 1: Posts data.
+                */
+               $this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
+
+               $this->assertSame( array(), _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are not cached.' );
+       }
+
+       /**
+        * @ticket 57163
+        */
+       public function test_prime_post_caches_only_posts_and_term_cache() {
+               $this->assertSame( self::$posts, _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are already cached.' );
+
+               $before_num_queries = get_num_queries();
+               _prime_post_caches( self::$posts, true, false );
+               $num_queries = get_num_queries() - $before_num_queries;
+
+               /*
+                * Three expected queries:
+                * 1: Posts data.
+                * 2: Taxonomy data,
+                * 3: Term data.
+                */
+               $this->assertSame( 3, $num_queries, 'Unexpected number of queries.' );
+
+               $this->assertSame( array(), _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are not cached.' );
+
+               // Test term cache.
+               $before_num_queries = get_num_queries();
+               $categories         = get_the_category( self::$posts[0] );
+               $num_queries        = get_num_queries() - $before_num_queries;
+
+               $this->assertNotEmpty( $categories, 'Categories does return an empty result set.' );
+               $this->assertSame( 0, $num_queries, 'Unexpected number of queries.' );
+       }
+
+       /**
+        * @ticket 57163
+        */
+       public function test_prime_post_caches_only_posts_and_meta_cache() {
+               $this->assertSame( self::$posts, _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are already cached.' );
+
+               $before_num_queries = get_num_queries();
+               _prime_post_caches( self::$posts, false, true );
+               $num_queries = get_num_queries() - $before_num_queries;
+
+               /*
+                * Two expected queries:
+                * 1: Posts data.
+                * 2: Post meta data.
+                */
+               $this->assertSame( 2, $num_queries, 'Unexpected number of queries warming cache.' );
+
+               $this->assertSame( array(), _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are not cached.' );
+
+               // Test post meta cache.
+               $before_num_queries = get_num_queries();
+               $meta_1             = get_post_meta( self::$posts[0], 'meta', true );
+               $meta_2             = get_post_meta( self::$posts[1], 'meta', true );
+               $num_queries        = get_num_queries() - $before_num_queries;
+
+               $this->assertSame( 'foo', $meta_1, 'Meta 1 has unexpected value.' );
+               $this->assertSame( 'bar', $meta_2, 'Meta 2 has unexpected value.' );
+               $this->assertSame( 0, $num_queries, 'Unexpected number of queries getting post meta.' );
+       }
+
+       /**
+        * @ticket 57163
+        */
+       public function test_prime_post_caches_accounts_for_posts_without_primed_meta_terms() {
+               $post_id = self::$posts[0];
+
+               $this->assertSame( array( $post_id ), _get_non_cached_ids( array( $post_id ), 'posts' ), 'Post is already cached.' );
+
+               // Warm only the posts cache.
+               $post = get_post( $post_id );
+               $this->assertNotEmpty( $post, 'Post does not exist.' );
+               $this->assertEmpty( _get_non_cached_ids( array( $post_id ), 'posts' ), 'Post is not cached.' );
+
+               $before_num_queries = get_num_queries();
+               _prime_post_caches( array( $post_id ) );
+               $num_queries = get_num_queries() - $before_num_queries;
+
+               /*
+                * Three expected queries:
+                * 1: Post meta data,
+                * 2: Taxonomy data,
+                * 3: Term data.
+                */
+               $this->assertSame( 3, $num_queries, 'Unexpected number of queries.' );
+       }
+
+       /**
+        * @ticket 57163
+        */
+       public function test_prime_post_caches_does_not_prime_caches_twice() {
+               $this->assertSame( self::$posts, _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are already cached.' );
+
+               _prime_post_caches( self::$posts );
+
+               $this->assertSame( array(), _get_non_cached_ids( self::$posts, 'posts' ), 'Posts are not cached.' );
+
+               $before_num_queries = get_num_queries();
+               _prime_post_caches( self::$posts );
+               $num_queries = get_num_queries() - $before_num_queries;
+
+               $this->assertSame( 0, $num_queries, 'Unexpected number of queries.' );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: branches/6.1/tests/phpunit/tests/post/primePostCaches.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches61testsphpunittestsquerycacheResultsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.1/tests/phpunit/tests/query/cacheResults.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.1/tests/phpunit/tests/query/cacheResults.php   2022-11-29 15:49:49 UTC (rev 54891)
+++ branches/6.1/tests/phpunit/tests/query/cacheResults.php     2022-11-29 20:09:24 UTC (rev 54892)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1237,9 +1237,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $query_1 = new WP_Query(
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'post_type' => 'page',
-                               'fields'    => $fields,
-                               'author'    => self::$author_id,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'post_type'              => 'page',
+                               'fields'                 => $fields,
+                               'author'                 => self::$author_id,
+                               'update_post_meta_cache' => false,
+                               'update_post_term_cache' => false,
</ins><span class="cx" style="display: block; padding: 0 10px">                         )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1247,6 +1249,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $start_loop_queries = get_num_queries();
</span><span class="cx" style="display: block; padding: 0 10px">                $query_1->the_post();
</span><span class="cx" style="display: block; padding: 0 10px">                $num_loop_queries = get_num_queries() - $start_loop_queries;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /*
+                * Two expected queries:
+                * 1: User meta data,
+                * 2: User data.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertSame( 2, $num_loop_queries, 'Unexpected number of queries while initializing the loop.' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $start_author_queries = get_num_queries();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1271,4 +1278,64 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * 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.' );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>