<!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>[56763] trunk: Query: Improve caching behavior for WP_Query when retrieving id=>parent fields</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/56763">56763</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/56763","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-10-03 14:59:22 +0000 (Tue, 03 Oct 2023)</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: Improve caching behavior for WP_Query when retrieving id=>parent fields
In <a href="https://core.trac.wordpress.org/changeset/53941">[53941]</a>, the addition of query caching to `WP_Query` brought about an unintended issue when querying for fields equal to id=>parent. Specifically, on websites with object caching enabled and a substantial number of pages, the second run of this query triggered the `_prime_post_caches` function for id=>parent. This led to the unnecessary priming of post, meta, and term caches, even when only id and parent information were requested.
This commit addresses this issue by introducing a new function, `_prime_post_parents_caches`, which primes a dedicated cache for post parents. This cache is primed during the initial query execution. Subsequently, the `wp_cache_get_multiple` function is employed to retrieve all post parent data in a single object cache request, optimizing performance.
Additionally, this commit extends the coverage of existing unit tests to ensure the reliability of the changes.
Props kevinfodness, joemcgill, peterwilsoncc, LinSoftware, thekt12, spacedmonkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/59188">#59188</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestsquerycacheResultsphp">trunk/tests/phpunit/tests/query/cacheResults.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestspostprimePostParentsCachesphp">trunk/tests/phpunit/tests/post/primePostParentsCaches.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php 2023-10-03 14:09:07 UTC (rev 56762)
+++ trunk/src/wp-includes/class-wp-query.php 2023-10-03 14:59:22 UTC (rev 56763)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3179,36 +3179,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $cached_results ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'ids' === $q['fields'] ) {
- /** @var int[] */
- $this->posts = array_map( 'intval', $cached_results['posts'] );
- } else {
- _prime_post_caches( $cached_results['posts'], $q['update_post_term_cache'], $q['update_post_meta_cache'] );
- /** @var WP_Post[] */
- $this->posts = array_map( 'get_post', $cached_results['posts'] );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** @var int[] */
+ $post_ids = array_map( 'intval', $cached_results['posts'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->post_count = count( $this->posts );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->post_count = count( $post_ids );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->found_posts = $cached_results['found_posts'];
</span><span class="cx" style="display: block; padding: 0 10px"> $this->max_num_pages = $cached_results['max_num_pages'];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'ids' === $q['fields'] ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->posts = $post_ids;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $this->posts;
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( 'id=>parent' === $q['fields'] ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ _prime_post_parents_caches( $post_ids );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /** @var int[] */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $post_parents = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $this->posts as $key => $post ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $post_parents as $id => $post_parent ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $obj = new stdClass();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $obj->ID = (int) $post->ID;
- $obj->post_parent = (int) $post->post_parent;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $obj->ID = (int) $id;
+ $obj->post_parent = (int) $post_parent;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->posts[ $key ] = $obj;
-
- $post_parents[ $obj->ID ] = $obj->post_parent;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->posts[] = $obj;
</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"> return $post_parents;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } else {
+ _prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
+ /** @var WP_Post[] */
+ $this->posts = array_map( 'get_post', $post_ids );
</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">@@ -3256,6 +3256,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $post_parents[ (int) $post->ID ] = (int) $post->post_parent;
</span><span class="cx" style="display: block; padding: 0 10px"> $post_ids[] = (int) $post->ID;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Prime post parent caches, so that on second run, there is not another database query.
+ wp_cache_add_multiple( $post_parents, 'post_parent' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $q['cache_results'] && $id_query_is_cacheable ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $cache_value = array(
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php 2023-10-03 14:09:07 UTC (rev 56762)
+++ trunk/src/wp-includes/functions.php 2023-10-03 14:59:22 UTC (rev 56763)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7169,7 +7169,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $cache_values = wp_cache_get_multiple( $object_ids, $cache_group );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $cache_values as $id => $value ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $value ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( false === $value ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $non_cached_ids[] = (int) $id;
</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="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2023-10-03 14:09:07 UTC (rev 56762)
+++ trunk/src/wp-includes/post.php 2023-10-03 14:59:22 UTC (rev 56763)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7262,6 +7262,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( $post->ID, 'posts' );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( $post->ID, 'post_meta' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_cache_delete( $post->ID, 'post_parent' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> clean_object_term_cache( $post->ID, $post->post_type );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7796,6 +7797,31 @@
</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">+ * Prime post parent caches.
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int[] $ids ID list.
+ */
+function _prime_post_parents_caches( array $ids ) {
+ global $wpdb;
+
+ $non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' );
+ if ( ! empty( $non_cached_ids ) ) {
+ $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.ID, $wpdb->posts.post_parent FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
+
+ if ( $fresh_posts ) {
+ $post_parent_data = array();
+ foreach ( $fresh_posts as $fresh_post ) {
+ $post_parent_data[ (int) $fresh_post->ID ] = (int) $fresh_post->post_parent;
+ }
+
+ wp_cache_add_multiple( $post_parent_data, 'post_parent' );
+ }
+ }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Adds a suffix if any trashed posts have a given slug.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Store its desired (i.e. current) slug so it can try to reclaim it
</span></span></pre></div>
<a id="trunktestsphpunittestspostprimePostParentsCachesphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/post/primePostParentsCaches.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/primePostParentsCaches.php (rev 0)
+++ trunk/tests/phpunit/tests/post/primePostParentsCaches.php 2023-10-03 14:59:22 UTC (rev 56763)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,132 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test `_prime_post_parents_caches()`.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Test class for `_prime_post_parents_caches()`.
+ *
+ * @group post
+ * @group cache
+ *
+ * @covers ::_prime_post_parents_caches
+ */
+class Tests_Post_PrimePostParentsCaches 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 );
+ }
+
+ /**
+ * @ticket 59188
+ */
+ public function test_prime_post_parents_caches() {
+ $post_id = self::$posts[0];
+
+ $before_num_queries = get_num_queries();
+ _prime_post_parents_caches( array( $post_id ) );
+ $num_queries = get_num_queries() - $before_num_queries;
+
+ $this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
+ $this->assertSameSets( array( 0 ), wp_cache_get_multiple( array( $post_id ), 'post_parent' ), 'Array of parent ids' );
+ }
+
+ /**
+ * @ticket 59188
+ */
+ public function test_prime_post_parents_caches_multiple() {
+ $before_num_queries = get_num_queries();
+ _prime_post_parents_caches( self::$posts );
+ $num_queries = get_num_queries() - $before_num_queries;
+
+ $this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
+ $this->assertSameSets( array( 0, 0, 0 ), wp_cache_get_multiple( self::$posts, 'post_parent' ), 'Array of parent ids' );
+ }
+
+ /**
+ * @ticket 59188
+ */
+ public function test_prime_post_parents_caches_multiple_runs() {
+ _prime_post_parents_caches( self::$posts );
+ $before_num_queries = get_num_queries();
+ _prime_post_parents_caches( self::$posts );
+ $num_queries = get_num_queries() - $before_num_queries;
+
+ $this->assertSame( 0, $num_queries, 'Unexpected number of queries.' );
+ }
+
+ /**
+ * @ticket 59188
+ */
+ public function test_prime_post_parents_caches_update() {
+ $page_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ 'post_parent' => self::$posts[0],
+ )
+ );
+ $before_num_queries = get_num_queries();
+ _prime_post_parents_caches( array( $page_id ) );
+ $num_queries = get_num_queries() - $before_num_queries;
+
+ $this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );
+ $this->assertSameSets( array( self::$posts[0] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 0 as parent' );
+
+ wp_update_post(
+ array(
+ 'ID' => $page_id,
+ 'post_parent' => self::$posts[1],
+ )
+ );
+
+ $before_num_queries = get_num_queries();
+ _prime_post_parents_caches( array( $page_id ) );
+ $num_queries = get_num_queries() - $before_num_queries;
+
+ $this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' );
+ $this->assertSameSets( array( self::$posts[1] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 1 as parent' );
+
+ }
+
+ /**
+ * @ticket 59188
+ */
+ public function test_prime_post_parents_caches_delete() {
+ $parent_page_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ )
+ );
+ $page_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ 'post_parent' => $parent_page_id,
+ )
+ );
+ $before_num_queries = get_num_queries();
+ _prime_post_parents_caches( array( $page_id ) );
+ $num_queries = get_num_queries() - $before_num_queries;
+
+ $this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );
+ $this->assertSameSets( array( $parent_page_id ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 0 as parent' );
+
+ wp_delete_post( $parent_page_id, true );
+
+ $this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' );
+ $this->assertSameSets( array( false ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with false values' );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/post/primePostParentsCaches.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="trunktestsphpunittestsquerycacheResultsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/query/cacheResults.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query/cacheResults.php 2023-10-03 14:09:07 UTC (rev 56762)
+++ trunk/tests/phpunit/tests/query/cacheResults.php 2023-10-03 14:59:22 UTC (rev 56763)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -710,7 +710,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $query2->query( $args );
</span><span class="cx" style="display: block; padding: 0 10px"> $queries_after = get_num_queries();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $queries_before, $queries_after );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( 1, $queries_after - $queries_before );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 5, $query1->posts );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 5, $query2->posts );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $query1->found_posts, $query2->found_posts );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -726,7 +726,111 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertNotEquals( $query1->posts, $query2->posts );
</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">+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 59188
+ */
+ public function test_query_cache_unprimed_parents() {
+ $args = array(
+ 'cache_results' => true,
+ 'fields' => 'id=>parent',
+ );
+ $query1 = new WP_Query();
+ $query1->query( $args );
+
+ $post_ids = wp_list_pluck( $query1->posts, 'ID' );
+ wp_cache_delete_multiple( $post_ids, 'post_parent' );
+
+ $queries_before = get_num_queries();
+ $query2 = new WP_Query();
+ $query2->query( $args );
+ $queries_after = get_num_queries();
+
+ $this->assertSame( 1, $queries_after - $queries_before, 'There should be only one query to prime parents' );
+ $this->assertCount( 5, $query1->posts, 'There should be only 5 posts returned on first query' );
+ $this->assertCount( 5, $query2->posts, 'There should be only 5 posts returned on second query' );
+ $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
+ }
+
+ /**
+ * @ticket 59188
+ */
+ public function test_query_cache_update_parent() {
+ $page_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ 'post_parent' => self::$pages[0],
+ )
+ );
+ $args = array(
+ 'cache_results' => true,
+ 'post_type' => 'page',
+ 'fields' => 'id=>parent',
+ 'post__in' => array(
+ $page_id,
+ ),
+ );
+ $query1 = new WP_Query();
+ $query1->query( $args );
+
+ wp_update_post(
+ array(
+ 'ID' => $page_id,
+ 'post_parent' => self::$pages[1],
+ )
+ );
+
+ $queries_before = get_num_queries();
+ $query2 = new WP_Query();
+ $query2->query( $args );
+ $queries_after = get_num_queries();
+
+ $this->assertSame( self::$pages[0], $query1->posts[0]->post_parent, 'Check post parent on first query' );
+ $this->assertSame( self::$pages[1], $query2->posts[0]->post_parent, 'Check post parent on second query' );
+ $this->assertSame( 2, $queries_after - $queries_before, 'There should be 2 queries, one for id=>parent' );
+ $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
+ }
+
+ /**
+ * @ticket 59188
+ */
+ public function test_query_cache_delete_parent() {
+ $parent_page_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ )
+ );
+ $page_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ 'post_parent' => $parent_page_id,
+ )
+ );
+ $args = array(
+ 'cache_results' => true,
+ 'post_type' => 'page',
+ 'fields' => 'id=>parent',
+ 'post__in' => array(
+ $page_id,
+ ),
+ );
+ $query1 = new WP_Query();
+ $query1->query( $args );
+
+ wp_delete_post( $parent_page_id, true );
+
+ $queries_before = get_num_queries();
+ $query2 = new WP_Query();
+ $query2->query( $args );
+ $queries_after = get_num_queries();
+
+ $this->assertSame( $parent_page_id, $query1->posts[0]->post_parent, 'Check post parent on first query' );
+ $this->assertSame( 0, $query2->posts[0]->post_parent, 'Check post parent on second query' );
+ $this->assertSame( 2, $queries_after - $queries_before, 'There should be 2 queries, one for id=>parent' );
+ $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 22176
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_query_cache_logged_in() {
</span></span></pre>
</div>
</div>
</body>
</html>