9/12/2018 - 4:56 PM

Regenerate image sizes for specific posts

Regenerate image sizes for specific posts

Regenerate specific image size for a post type


I have a custom post type called lfh_home, which uses an image size called card-header on post listing pages.

When I make changes to that image size for testing, I want to regenerate the thumbnails/generated media, but don't want to do it to all 3000+ images on the site.

The goal here is to get the posts of the lfh_home post type, get the ID of the featured image (stored in the _thumbnail_id post meta field), and regenerate just the card-header image.

A regular wp media regenerate --image_size=card-header task takes approximately 12m24s to process 2971 images in local testing. This script takes 44s to process 10 images on the same machine.


  1. Put the script into a directory you can run commands from using WP-CLI.
  2. Make the script executable with chmod +x
  3. Run ./ <post_type> <image_size>, where post_type is your post type (default 'post') and image_size is an image size registered with WordPress (default 'thumbnail').