Query: Prevent ID only queries erroring when starting the loop.

Ensure only full post objects are passed to `update_post_author_caches()` when called within `WP_Query::the_post()`. This prevents an error when starting the Loop for Queries initiated with a subset of fields or IDs only.

Props konyoldeath, dd32, lozula, TimothyBlynJacobs, spacedmonkey, mxbclang, peterwilsoncc.
Fixes #56948.


Built from https://develop.svn.wordpress.org/trunk@54771


git-svn-id: http://core.svn.wordpress.org/trunk@54323 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2022-11-09 00:59:12 +00:00
parent 330c35b258
commit 113838ed78
3 changed files with 19 additions and 2 deletions

View File

@ -3586,7 +3586,15 @@ class WP_Query {
global $post;
if ( ! $this->in_the_loop ) {
update_post_author_caches( $this->posts );
// Only prime the post cache for queries limited to the ID field.
$post_ids = array_filter( $this->posts, 'is_numeric' );
// Exclude any falsey values, such as 0.
$post_ids = array_filter( $post_ids );
if ( $post_ids ) {
_prime_post_caches( $post_ids, $this->query_vars['update_post_term_cache'], $this->query_vars['update_post_meta_cache'] );
}
$post_objects = array_map( 'get_post', $this->posts );
update_post_author_caches( $post_objects );
}
$this->in_the_loop = true;

View File

@ -7461,6 +7461,15 @@ function update_post_caches( &$posts, $post_type = 'post', $update_term_cache =
* @param WP_Post[] $posts Array of post objects.
*/
function update_post_author_caches( $posts ) {
/*
* cache_users() is a pluggable function so is not available prior
* to the `plugins_loaded` hook firing. This is to ensure against
* fatal errors when the function is not available.
*/
if ( ! function_exists( 'cache_users' ) ) {
return;
}
$author_ids = wp_list_pluck( $posts, 'post_author' );
$author_ids = array_map( 'absint', $author_ids );
$author_ids = array_unique( array_filter( $author_ids ) );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-54770';
$wp_version = '6.2-alpha-54771';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.