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" --fields=ID

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

for id in $(wp post list --ping_status="open" --fields=ID); 
do
  wp post update $id --ping_status="closed";
done

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"

Update meta where it’s missing.

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" "meta_value"
done

Update post meta for all posts in a category.

for id in $(wp post list --category_name="category" --fields=ID);
do
  wp post meta update $id "meta_key" "meta_value";
done

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

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 posts with specific meta key.

for id in $( wp post list --fields=ID --meta_key="meta_key" );
do
  wp post delete $id;
done

Delete posts with specific meta key and meta value.

for id in $( wp post list --fields=ID --meta_key="meta_key" --meta_value="meta_value" );
do
  wp post delete $id;
done

Delete posts where a specific meta key is missing.

for id in $( wp post list --fields=ID --meta_key="meta_key" --meta_compare="NOT EXISTS" );
do
  wp post delete $id;
done

Site options

Documentation

Change the site URL.

wp option update home 'http://example.com'
wp option update siteurl 'http://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

Browse all