Skip to content

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

Closed

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.

}

Activity

  1. danielbachhuber commented on Oct 11, 2023

    @danielbachhuber
    Member

    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.

  2. oxyc commented on Feb 14, 2024

    @oxyc
    Contributor

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

  3. danielbachhuber commented on Feb 21, 2024

    @danielbachhuber
    Member

    🔨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions