Skip to content

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

@claytoncollie

Description

@claytoncollie

'posts_per_page' => -1,

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.

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions