Assign a featured image to a generated post

Use xargs to chain together wp post generate --format=ids and wp post meta update.

WP-CLI lets you generate dummy posts with wp post generate (doc). By default, these generated posts don’t include a featured image.

To assign a featured image to generated posts, you’ll need to pass the ids for the generated posts to wp post meta update:

wp post generate --format=ids | xargs -0 -d ' ' -I % wp post meta update % _thumbnail_id <attachment-id>

Replace <attachment-id> with the id for the image attachment you’d like to assign as the featured image for each post.

Deconstructing this example:

  • wp post generate --format=ids generates 100 new posts, and outputs the ids for those new posts.
  • The | operator passes the results from wp post generate to xargs, a utility which takes lines of input and passes them to some command.
  • xargs -0 -d ' ' -I % wp post meta update % _thumbnail_id <attachment-id> runs wp post meta update _thumbnail_id <attachment-id> against each generated post, where % is a placeholder for the post id and <attachment-id> is the id for the image attachment you’d like to assign.