Skip to content

Commit d3b4d78

Browse files
authored
Optimize memory consumption of querying attachments (#188)
1 parent 3a5d9a5 commit d3b4d78

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

‎src/Media_Command.php‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ public function regenerate( $args, $assoc_args = array() ) {
160160
$successes = 0;
161161
$errors = 0;
162162
$skips = 0;
163-
foreach ( $images->posts as $post ) {
163+
foreach ( $images->posts as $post_id ) {
164164
++$number;
165165
if ( 0 === $number % self::WP_CLEAR_OBJECT_CACHE_INTERVAL ) {
166166
Utils\wp_clear_object_cache();
167167
}
168-
$this->process_regeneration( $post->ID, $skip_delete, $only_missing, $image_size, $number . '/' . $count, $successes, $errors, $skips );
168+
$this->process_regeneration( $post_id, $skip_delete, $only_missing, $image_size, $number . '/' . $count, $successes, $errors, $skips );
169169
}
170170

171171
if ( $image_size ) {
@@ -953,11 +953,14 @@ private function get_images( $args = array(), $additional_mime_types = array() )
953953
$mime_types = array_merge( array( 'image' ), $additional_mime_types );
954954

955955
$query_args = array(
956-
'post_type' => 'attachment',
957-
'post__in' => $args,
958-
'post_mime_type' => $mime_types,
959-
'post_status' => 'any',
960-
'posts_per_page' => -1,
956+
'post_type' => 'attachment',
957+
'post__in' => $args,
958+
'post_mime_type' => $mime_types,
959+
'post_status' => 'any',
960+
'posts_per_page' => -1,
961+
'fields' => 'ids',
962+
'update_post_meta_cache' => false,
963+
'update_post_term_cache' => false,
961964
);
962965

963966
return new WP_Query( $query_args );
@@ -1071,12 +1074,12 @@ public function fix_orientation( $args, $assoc_args ) {
10711074
$number = 0;
10721075
$successes = 0;
10731076
$errors = 0;
1074-
foreach ( $images->posts as $post ) {
1077+
foreach ( $images->posts as $post_id ) {
10751078
++$number;
10761079
if ( 0 === $number % self::WP_CLEAR_OBJECT_CACHE_INTERVAL ) {
10771080
Utils\wp_clear_object_cache();
10781081
}
1079-
$this->process_orientation_fix( $post->ID, "{$number}/{$count}", $successes, $errors, $dry_run );
1082+
$this->process_orientation_fix( $post_id, "{$number}/{$count}", $successes, $errors, $dry_run );
10801083
}
10811084

10821085
if ( Utils\get_flag_value( $assoc_args, 'dry-run' ) ) {

0 commit comments

Comments
 (0)