Useful WP-CLI commands and scripts

I use WordPress at work and also for some of my personal projects, including the very site you’re on right now. One tool that’s been very handy is WP-CLI, the official command-line interface for WordPress. It’s particularly useful in combination with bash scripts.

Here’s my collection of useful WP-CLI commands that I’ve built over the years, both for my own use and some of the more advanced ones might save you a little bit of time.

Feel free to reach out and share your own.

Basic work with posts

Documentation

Search post content.

wp post list --s="text" --fields=ID,post_title,url

Find posts with ping status “open”.

wp post list --ping_status="open"

And if you only want to get the post IDs:

wp post list --ping_status="open" --fields=ID

Update ping status to “closed” where the value is “open”.

wp post update $(wp post list --ping_status="open" --format=ids) --ping_status="closed";

Search and replace

Documentation

Search and replace post content.

wp search-replace "old thing" "new thing" --all-tables

Post meta

Documentation

List all metadata associated with a post.

wp post meta list 1422 --format=json

Get specific post meta for a post.

wp post meta get 1422 "meta_key" --format=json

Update post meta.

wp post meta update 1422 "meta_key" "meta_value"

Update post meta from a file.

wp post meta update 1422 "post_meta" < file.json --format=json

Delete post meta.

wp post meta delete 1422 "post_meta"

List posts by meta key or value.

wp post list --fields=ID,post_title,url --meta_key="meta_key"
wp post list --fields=ID,post_title,url --meta_key="meta_key" --meta_compare="NOT EXISTS"
wp post list --post_type="post_type" --fields=ID,post_title,url --meta_key="meta_key" --meta_compare="NOT EXISTS"
wp post list --fields=ID,post_title,url --meta_key="_wp_page_template" --meta_value="page-templates/post-full-grid.php"

The meta_compare flag also accepts "LIKE" as a value, so that you can do:

wp post list --fields=ID,post_title,url --meta_key='meta_key' --meta_compare='LIKE' --meta_value='value'

Update meta where it’s missing.

wp post meta update $(wp post list --post_type="post_type" --meta_key="meta_key" --meta_compare="NOT EXISTS" --format=ids) "meta_key" "meta_value"

Update post meta for all posts in a category.

wp post meta update $(wp post list --category_name="category" --format=ids) "meta_key" "meta_value"

Save meta value based on an existing meta value. In this example I’m looping through all post with a specific post type and saving the URL of the featured image to the post’s meta.

for id in $(wp post list --post_type="post_type" --fields=ID --meta_key="meta_key" --meta_compare="NOT EXISTS")
do
    wp post meta update $id "meta_key" $(wp post meta pluck $(wp post meta get $id _thumbnail_id) _wp_attachment_metadata file)
done

Show specific meta value for posts based on a category.

for id in $(wp post list --post_type="post_type" --fields=ID --category="category name")
do
    wp post meta get $id "meta_key"
done

Export/import all meta.

wp post meta list 1422 --format=json > 1422_meta.json
wp post update 1422 --meta_input= < 1422_meta.json

Delete posts

Documentation

Delete all posts from a custom post type. (Adding --force will skip the trash and remove the posts completely.)

wp post delete $(wp post list --post_type='activity' --format=ids) --force

If you’re trying to delete too many posts at once, you might get an error like this:

PHP Fatal error: Allowed memory size of xxx bytes exhausted

Or perhaps this one:

Argument list too long

One way around this is to limit how many posts you’re deleting at once using the posts_per_page option, for example:

wp post delete $(wp post list --post_type='activity' --posts_per_page=50000 --format=ids) --force

Delete posts with specific meta key.

wp post delete $(wp post list --format=ids --meta_key="meta_key")

Delete posts with specific meta key and meta value.

wp post delete $(wp post list --format=ids --meta_key="meta_key" --meta_value="meta_value")

Delete posts where a specific meta key is missing.

wp post delete $(wp post list --format=ids --meta_key="meta_key" --meta_compare="NOT EXISTS")

Post revisions

List all revisions.

wp post list --post_type=revision

List revisions for a specific post.

wp post list --post_parent=1422 --post_type=revision

Get more info on revisions for a specific post.

for id in $(wp post list --post_parent=1422 --post_type=revision --fields=ID)
do
    wp post get $id
done

Site options

Documentation

Change the site URL.

wp option update home 'https://example.com'
wp option update siteurl 'https://example.com'

Plugins

Documentation

List active plugins.

wp plugin list --status=active

Activate/deactivate plugins.

wp plugin activate plugin-1 plugin-2 plugin-3
wp plugin deactivate plugin-1 plugin-2 plugin-3

Users

Documentation

Update user password.

wp user update USER_ID --user_pass="newpassword"

More tutorials

A tinted, zoomed in screenshot of a JSON object showing server information about a Mastodon instance.
A tinted screenshot of two charts, one showing the popularity of various fediverse platforms (with Mastodon far ahead of the rest), and the other chart showing distribution of domain creation dates, mostly clustered around 2023.
A tinted screenshot showing the @mtaupdates Mastodon profile and a few example posts with subway status alerts.

💻 Browse all