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¶
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";
Post meta¶
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.
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
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¶
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")
Site options¶
Change the site URL.
wp option update home 'http://example.com'
wp option update siteurl 'http://example.com'
Plugins¶
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¶
Update user password.
wp user update USER_ID --user_pass="newpassword"