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:
Dominik Schilling
2022-11-29 20:27:29 +00:00
parent e50c65caf4
commit 35421f2b07
3 changed files with 316 additions and 5 deletions

View File

@@ -7876,7 +7876,9 @@ function _update_term_count_on_transition_post_status( $new_status, $old_status,
* @since 3.4.0
* @since 6.1.0 This function is no longer marked as "private".
*
* @see update_post_caches()
* @see update_post_cache()
* @see update_postmeta_cache()
* @see update_object_term_cache()
*
* @global wpdb $wpdb WordPress database abstraction object.
*
@@ -7891,7 +7893,20 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
if ( ! empty( $non_cached_ids ) ) {
$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
if ( $fresh_posts ) {
// Despite the name, update_post_cache() expects an array rather than a single post.
update_post_cache( $fresh_posts );
}
}
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 );
}
}