Query: Cache post parent IDs in posts group.

Move the cache of post parent IDs from the dedicated group `post_parents` to `posts`. This maintains backward compatibility for clearing all post object related data by calling `wp_cache_flush_group( 'posts' )`.

Post parent IDs are now cached with with the prefix `post_parent:` in the `posts` group.

Follow up to [56763].

Props spacedmonkey, joemcgill, peterwilsoncc.
See #59188.


git-svn-id: https://develop.svn.wordpress.org/trunk@56925 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2023-10-12 23:39:05 +00:00
parent 9fb592eaa1
commit ac0bae2359
4 changed files with 61 additions and 19 deletions
+15 -7
View File
@@ -3192,12 +3192,17 @@ class WP_Query {
} elseif ( 'id=>parent' === $q['fields'] ) {
_prime_post_parent_id_caches( $post_ids );
/** @var int[] */
$post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' );
$post_parent_cache_keys = array();
foreach ( $post_ids as $post_id ) {
$post_parent_cache_keys[] = 'post_parent:' . (string) $post_id;
}
foreach ( $post_parents as $id => $post_parent ) {
/** @var int[] */
$post_parents = wp_cache_get_multiple( $post_parent_cache_keys, 'posts' );
foreach ( $post_parents as $cache_key => $post_parent ) {
$obj = new stdClass();
$obj->ID = (int) $id;
$obj->ID = (int) str_replace( 'post_parent:', '', $cache_key );
$obj->post_parent = (int) $post_parent;
$this->posts[] = $obj;
@@ -3245,8 +3250,9 @@ class WP_Query {
$this->set_found_posts( $q, $limits );
/** @var int[] */
$post_parents = array();
$post_ids = array();
$post_parents = array();
$post_ids = array();
$post_parents_cache = array();
foreach ( $this->posts as $key => $post ) {
$this->posts[ $key ]->ID = (int) $post->ID;
@@ -3254,9 +3260,11 @@ class WP_Query {
$post_parents[ (int) $post->ID ] = (int) $post->post_parent;
$post_ids[] = (int) $post->ID;
$post_parents_cache[ 'post_parent:' . (string) $post->ID ] = (int) $post->post_parent;
}
// Prime post parent caches, so that on second run, there is not another database query.
wp_cache_add_multiple( $post_parents, 'post_parent' );
wp_cache_add_multiple( $post_parents_cache, 'posts' );
if ( $q['cache_results'] && $id_query_is_cacheable ) {
$cache_value = array(