Skip to content

Commit c17ac06

Browse files
committed
Perf improve.
1 parent 8451ebc commit c17ac06

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/wp-includes/class-wp-query.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3739,20 +3739,24 @@ public function the_post() {
37393739
global $post;
37403740

37413741
if ( ! $this->in_the_loop ) {
3742-
if ( 'all' === $this->query_vars['fields'] ) {
3742+
if ( $this->posts[0] instanceof WP_Post ) {
3743+
// Full post objects queried.
37433744
$post_objects = $this->posts;
37443745
} else {
37453746
// Only partial objects queried, need to prime the cache for the loop.
3746-
$post_ids = array();
3747-
foreach ( $this->posts as $loop_post ) {
3748-
if ( 'ids' === $this->query_vars['fields'] ) {
3749-
$post_ids[] = $loop_post;
3750-
} else {
3751-
// Partial object (stdClass) queried.
3752-
$post_ids[] = $loop_post->ID;
3753-
}
3754-
}
3747+
$post_ids = array_reduce(
3748+
$this->posts,
3749+
function ( $carry, $post ) {
3750+
if ( 'ids' === $this->query_vars['fields'] ) {
3751+
$carry[] = $post;
3752+
} elseif ( isset( $post->ID ) ) {
3753+
$carry[] = $post->ID;
3754+
}
37553755

3756+
return $carry;
3757+
},
3758+
array()
3759+
);
37563760
_prime_post_caches( $post_ids, $this->query_vars['update_post_term_cache'], $this->query_vars['update_post_meta_cache'] );
37573761
$post_objects = array_map( 'get_post', $post_ids );
37583762
}
@@ -3776,12 +3780,14 @@ public function the_post() {
37763780
$post = $this->next_post();
37773781

37783782
// Ensure a full post object is available.
3779-
if ( 'ids' === $this->query_vars['fields'] ) {
3780-
// Post IDs queried.
3781-
$post = get_post( $post );
3782-
} elseif ( 'all' !== $this->query_vars['fields'] ) {
3783-
// Partial object (stdClass) queried.
3784-
$post = get_post( $post->ID );
3783+
if ( ! $post instanceof WP_Post ) {
3784+
if ( 'ids' === $this->query_vars['fields'] ) {
3785+
// Post IDs queried.
3786+
$post = get_post( $post );
3787+
} elseif ( isset( $post->ID ) ) {
3788+
// Partial object (stdClass) queried.
3789+
$post = get_post( $post->ID );
3790+
}
37853791
}
37863792

37873793
// Set up the global post object for the loop.

0 commit comments

Comments
 (0)