Skip to content

Commit 922c519

Browse files
committed
Always clear the object cache after each chunk of posts
wp-cli/wp-cli#6426 makes the formatter read the requested fields of each item while it is current, also when it can't stream them. Post meta is therefore read before the cache of its chunk is cleared, so the cache no longer needs to be kept when computed fields are listed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS
1 parent c3563e6 commit 922c519

1 file changed

Lines changed: 7 additions & 17 deletions

File tree

‎src/Post_Command.php‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,8 @@ public function list_( $args, $assoc_args ) {
10621062
$query = new WP_Query( $query_args );
10631063
$formatter->display_items( $query->posts ?? [] );
10641064
} else {
1065-
$fields = $formatter->field ? [ $formatter->field ] : (array) $formatter->fields;
1066-
$formatter->display_items( $this->query_posts_in_chunks( $query_args, $fields ) );
1065+
$need_url = 'url' === $formatter->field || in_array( 'url', (array) $formatter->fields, true );
1066+
$formatter->display_items( $this->query_posts_in_chunks( $query_args, $need_url ) );
10671067
}
10681068
}
10691069

@@ -1075,22 +1075,14 @@ public function list_( $args, $assoc_args ) {
10751075
* chunk. Formats that can be written item by item are then streamed by the formatter.
10761076
*
10771077
* Fields that WP_Post computes on access, like post meta, are read from the object
1078-
* cache. If any of them are displayed, the cache is kept, because the formatter may
1079-
* only read them once all posts have been loaded.
1078+
* cache. The formatter reads the requested fields of each post while it is the current
1079+
* item, before the cache of its chunk is cleared, even when it can't stream them.
10801080
*
10811081
* @param array<string, mixed> $query_args WP_Query arguments.
1082-
* @param string[] $fields Fields that will be displayed.
1082+
* @param bool $need_url Whether to add the `url` property to each post.
10831083
* @return \Generator<int, \WP_Post>
10841084
*/
1085-
private function query_posts_in_chunks( $query_args, $fields ) {
1086-
$need_url = in_array( 'url', $fields, true );
1087-
$clear_cache = true;
1088-
foreach ( $fields as $field ) {
1089-
if ( 'url' !== $field && ! property_exists( 'WP_Post', $field ) && ! property_exists( 'WP_Post', 'post_' . $field ) ) {
1090-
$clear_cache = false;
1091-
}
1092-
}
1093-
1085+
private function query_posts_in_chunks( $query_args, $need_url ) {
10941086
/** @var int[] $ids */
10951087
$ids = ( new WP_Query( array_merge( $query_args, [ 'fields' => 'ids' ] ) ) )->posts;
10961088

@@ -1129,9 +1121,7 @@ private function query_posts_in_chunks( $query_args, $fields ) {
11291121
}
11301122

11311123
unset( $posts, $query );
1132-
if ( $clear_cache ) {
1133-
self::clear_runtime_object_cache();
1134-
}
1124+
self::clear_runtime_object_cache();
11351125
}
11361126
}
11371127

0 commit comments

Comments
 (0)