diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index 3833a7a5a5..a52b5a484e 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -3179,36 +3179,36 @@ class WP_Query { $cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found ); if ( $cached_results ) { - 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'] ); - } + /** @var int[] */ + $post_ids = array_map( 'intval', $cached_results['posts'] ); - $this->post_count = count( $this->posts ); + $this->post_count = count( $post_ids ); $this->found_posts = $cached_results['found_posts']; $this->max_num_pages = $cached_results['max_num_pages']; if ( 'ids' === $q['fields'] ) { + $this->posts = $post_ids; + return $this->posts; } elseif ( 'id=>parent' === $q['fields'] ) { + _prime_post_parents_caches( $post_ids ); + /** @var int[] */ - $post_parents = array(); + $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' ); - foreach ( $this->posts as $key => $post ) { + foreach ( $post_parents as $id => $post_parent ) { $obj = new stdClass(); - $obj->ID = (int) $post->ID; - $obj->post_parent = (int) $post->post_parent; + $obj->ID = (int) $id; + $obj->post_parent = (int) $post_parent; - $this->posts[ $key ] = $obj; - - $post_parents[ $obj->ID ] = $obj->post_parent; + $this->posts[] = $obj; } return $post_parents; + } 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 ); } } } @@ -3256,6 +3256,8 @@ class WP_Query { $post_parents[ (int) $post->ID ] = (int) $post->post_parent; $post_ids[] = (int) $post->ID; } + // Prime post parent caches, so that on second run, there is not another database query. + wp_cache_add_multiple( $post_parents, 'post_parent' ); if ( $q['cache_results'] && $id_query_is_cacheable ) { $cache_value = array( diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 4b6bc14acf..bb799802ad 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -7169,7 +7169,7 @@ function _get_non_cached_ids( $object_ids, $cache_group ) { $cache_values = wp_cache_get_multiple( $object_ids, $cache_group ); foreach ( $cache_values as $id => $value ) { - if ( ! $value ) { + if ( false === $value ) { $non_cached_ids[] = (int) $id; } } diff --git a/wp-includes/post.php b/wp-includes/post.php index 8a3bc56656..ea81bbd045 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -7262,6 +7262,7 @@ function clean_post_cache( $post ) { wp_cache_delete( $post->ID, 'posts' ); wp_cache_delete( $post->ID, 'post_meta' ); + wp_cache_delete( $post->ID, 'post_parent' ); clean_object_term_cache( $post->ID, $post->post_type ); @@ -7795,6 +7796,31 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache } } +/** + * 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' ); + } + } +} + /** * Adds a suffix if any trashed posts have a given slug. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 4ad00b033e..57ce8593b6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-beta1-56762'; +$wp_version = '6.4-beta1-56763'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.