morganestes
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

Background

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.

Usage

  1. Put the script into a directory you can run commands from using WP-CLI.
  2. Make the script executable with chmod +x regenerate-images-for-post-type.sh
  3. Run ./regenerate-images-for-post-type.sh <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').