@@ -1057,8 +1057,8 @@ public function list_( $args, $assoc_args ) {
10571057 $ query = new WP_Query ( $ query_args );
10581058 $ formatter ->display_items ( $ query ->posts ?? [] );
10591059 } else {
1060- $ fields = $ formatter -> field ? [ $ formatter ->field ] : (array ) $ formatter ->fields ;
1061- $ formatter ->display_items ( $ this ->query_posts_in_chunks ( $ query_args , $ fields ) );
1060+ $ need_url = ' url ' === $ formatter ->field || in_array ( ' url ' , (array ) $ formatter ->fields , true ) ;
1061+ $ formatter ->display_items ( $ this ->query_posts_in_chunks ( $ query_args , $ need_url ) );
10621062 }
10631063 }
10641064
@@ -1070,22 +1070,14 @@ public function list_( $args, $assoc_args ) {
10701070 * chunk. Formats that can be written item by item are then streamed by the formatter.
10711071 *
10721072 * Fields that WP_Post computes on access, like post meta, are read from the object
1073- * cache. If any of them are displayed, the cache is kept, because the formatter may
1074- * only read them once all posts have been loaded .
1073+ * cache. The formatter reads the requested fields of each post while it is the current
1074+ * item, before the cache of its chunk is cleared, even when it can't stream them .
10751075 *
10761076 * @param array<string, mixed> $query_args WP_Query arguments.
1077- * @param string[] $fields Fields that will be displayed .
1077+ * @param bool $need_url Whether to add the `url` property to each post .
10781078 * @return \Generator<int, \WP_Post>
10791079 */
1080- private function query_posts_in_chunks ( $ query_args , $ fields ) {
1081- $ need_url = in_array ( 'url ' , $ fields , true );
1082- $ clear_cache = true ;
1083- foreach ( $ fields as $ field ) {
1084- if ( 'url ' !== $ field && ! property_exists ( 'WP_Post ' , $ field ) && ! property_exists ( 'WP_Post ' , 'post_ ' . $ field ) ) {
1085- $ clear_cache = false ;
1086- }
1087- }
1088-
1080+ private function query_posts_in_chunks ( $ query_args , $ need_url ) {
10891081 /** @var int[] $ids */
10901082 $ ids = ( new WP_Query ( array_merge ( $ query_args , [ 'fields ' => 'ids ' ] ) ) )->posts ;
10911083
@@ -1124,9 +1116,7 @@ private function query_posts_in_chunks( $query_args, $fields ) {
11241116 }
11251117
11261118 unset( $ posts , $ query );
1127- if ( $ clear_cache ) {
1128- self ::clear_runtime_object_cache ();
1129- }
1119+ self ::clear_runtime_object_cache ();
11301120 }
11311121 }
11321122
0 commit comments