Cartoon of Author
via Mastodon


Useful WP-CLI Tricks I Often Use


I love WP-CLI because it saves me a ton of time, especially when managing multiple sites quickly. I'm always looking for useful tricks with it to make my life easier. I haven't posted in quite some time - so - I figured I'd make a quick little list of my lesser known WP-CLI commands that I frequently use.

Update Admin Email (Without Outbound Email Confirmation)

wp option update admin_email [email protected]

A few versions back WordPress removed the ability to update the admin email without sending an email confirmation to the new address. I often find with clients that they just don't accept the email, so, I love this little handy trick. This will forcefully update the admin email without needing them to confirm. The only gotcha is that it switches the old email to be "Pending Confirmation" but you can easily clear that out by going to Settings > General and hitting "Cancel".

Delete All WooCommerce Orders

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

Does exactly what it says and purges all of the orders in a WooCommerce Store. Though you can actually use it with any post type (by updating the --post_type="shop_order" to whatev) - but - for me the most frequent use is purging out a store.

Delete All Customers from WooCommerce

wp user delete $(wp user list --role="customer" --field=ID) --reassign={your-user-id}

This one will purge anybody with the 'Customer' role in WordPress (the default role in WooCommerce). You could also replace the --role="customer" with another role to purge something else. (Like Subscribers). Be sure to set your ID under the --reassign argument if you don't want the user data to be lost.

Delete All Content on a WordPress Install

wp site empty

This one is fun if you're making a quick clone of a blueprint, it'll purge all of your posts, comments, and terms quickly without erasing the site configuration like wp db reset would. I might recommend running wp db export for a quick backup before giving this a go.

You can also toss --uploads at the end there as well to delete your upload folder files. (wp db export isn't saving you here, so, be careful!)

List Post Meta Data

wp post meta list {id}

Useful if you need to see what meta data is attached to a specific post (or page, or shop order, etc).

Delete Spam Comments

wp comment delete $(wp comment list --status=spam --format=ids)

I loath comments, I disable them on every site I build unless I'm asked to turn them on. Whenever I do have to enable them I almost always find myself dealing with spam at some point. My go-to is just to delete everything, which this does!

Regenerate Media Thumbnails

wp media regenerate --yes

If you've ever added a new thumbnail size, updated your thumbnail settings in WooCommerce, or similar you'll know it doesn't retroactively update things. Running this simple command will regenrate them quickly-ish. (Depending on how many images/thumbnails you have)

Update ALL THE THINGS (Core, Themes, Plugins)

wp core update && wp plugin update --all && wp theme update --all

That's technically three commands, but, still pretty useful if you're just looking to bulk update everything with a one liner, I use it whenever I clone out a blueprint that I've been too lazy to update. Probs wouldn't do it on production, unless you're 100% sure you'll have no issues/conflicts.

Purge Beaver Builder / Themer Cache

wp beaver clearcache

I use Beaver Builder a lot but one of the issues I have with it is when I push up from my local to staging the layouts Beaver Builder generate like to cache my .local domain causing a bunch of images to be broken. By the time I'm pushing something up I'm tired of working on it, so the last thing I want to do is login and click the "Clear Cache" button. This does it for me, which is nice.

That's all I've got for now - though - I might update this later if I find anything else that seems out of the ordinary. Obviously there's loads you can (and I do!) with WP-CLI, but, I've found these tricks to be lesser known. 😊