wp-cli / media-command

Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
MIT License
44 stars 41 forks source link

This script times out on installs with large number of attachments #134

Closed claytoncollie closed 6 months ago

claytoncollie commented 4 years ago

https://github.com/wp-cli/media-command/blob/830e72a2cbd3eeec95a97df2c1c17d925d86790d/src/Media_Command.php#L910

I used this script a few months ago on an install with somewhere around 12k attachments. I updated the theme and needed to resize all of my images. I initially used Regenerate Thumbnails but that timed out my login session and failed after a while. I then used this script which went a little faster but ultimately timed out and failed. I didn't think much of it then and kept running the script to see if I could make it all the way through my images.

Now that I am building a command for another client and using this script as an example, I am starting to think more about the WP_Query that gets all posts. To make this more scalable, should this query be changed to a paged query inside a foreach loop based on the number of total posts for this post type?

Something like

$total = 0;
$count = wp_count_posts( 'attachment' );

if ( ! empty( $count ) ) {

    foreach( $count as $post_status => $posts ) {
        $total += $posts;
    }

}

$posts_per_page = 100;

$pages = ceil( $total / $posts_per_page );

for($page = 1; $page <= $pages; $page++) {

    $query = new WP_Query(
        array(
            'post_type'           => 'attachment',
            'post_status'        => 'any',
            'posts_per_page' => $posts_per_page,
            'paged'              => $page
        )
    );

        // Do loop here.

}
danielbachhuber commented 11 months ago

Thanks for the suggestion, @claytoncollie

I think I'd avoid a full refactoring and simply add some memory management while it's iterating through attachments.

oxyc commented 7 months ago

I got 500k attachments :) guess ill just write a wrapper then

danielbachhuber commented 6 months ago

🔨