A simple WP-CLI command to query posts for their content and set the first found image as the post thumbnail.

0.1.0 2020-12-06 17:11 UTC

This package is auto-updated.

Last update: 2021-01-06 17:28:15 UTC


A simple WP-CLI command to query posts for their content and set the first found image as the post thumbnail.



There are two preferred ways to install this package:

First, using WP-CLI's package installer command. Simply run wp package install jmichaelward/set-post-thumbs in the command line, then you're good to go.

Second, you can similarly install it using Composer. In the terminal, enter:

composer require jmichaelward/set-post-thumbs

Assuming you're already using Composer's class autoloader in your WordPress project, then you can then call the function JMichaelWard\SetPostThumbs\init_thumbnail_command() inside an mu-plugins PHP file, an active plugin, or your theme's functions.php file.


Alternately, you can clone down this repository. Then, require the set-post-thumbs.php file relative to your file path. This will make the JMichaelWard\SetPostThumbs\init_thumbnail_command() method available to your project.


Once active, this package registers a thumbnail command with WP-CLI. A brief synopsis of the available subcommands is below. Please run wp help thumbnail to see the full set of options.

wp thumbnail set [--all] [--amount=<amount>] [--post_type=<post_type>]

Without options, this will attempt to set a post thumbnail on the first 500 posts WordPress can find without a featured image. You may request that this processes on all posts by passing the --all flag, or a specified amount of posts by using --amount=<amount>. Posts are the default post type, but you can specify other post types with the --post_type flag.

wp thumbnail show <unset|multiple> [--post_type=<post_type>]

Passing unset will return a list of post IDs that were processed, but which the command was unable to set a featured image.

Passing multiple will return a list of post IDs that were processed and where a featured image was set, but where the posts contained multiple images in the content.

As before, passing the --post_type flag will query the selected post type.

wp thumbnail cleanup

This command queries all posts which have metadata generated by the above commands, then subsequently removes that metadata. I recommend running this command after assigning your featured images with this tool to clear out the unnecessary metadata, as it's primarily used for reporting with the wp thumbnail show command.

Extending the command

The ThumbnailCommand class located in the src/ directory makes the above commands available to you. By default, the thumbnail command looks for images within your post content and assigns the first one it finds as the post's featured image. However, your site may have image data located in other places that you may wish to use to set as a featured image. The ThumbnailCommand class is designed to be extended so that you can create your own command from this scenario. The maybe_set_featured_image method should be your primary starting point for extension.