Editing WordPress using WP-CLI

Wordpress Resource Center

Cheat sheet, code snippets, call it what you want.  Here’s a list of WP-CLI  code snippets that we use a lot around here.

WooCommerce Related Snippets

Delete all WooCommerce product categories

wp term list product_cat --field=term_id | xargs wp term delete product_cat

Delete all products in “draft” (NOTE THIS ACTUALLY DELETES ALL PUBLISHED PRODUCTS)

wp post delete $(wp post list --post_type='product' --format=ids) --force --defer-term-counting

Media Related Snippets 

Regenerate all thumbnails without confirmation

wp media regenerate --yes

Delete all media files that are not attached. Useful for removing product images when deleting all products.

wp post delete $(wp post list --post_type='attachment' --format=ids --post_parent=0)

OR

wp post delete $(wp post list --post_type='attachment' --format=ids --post_parent=0 --post_mime_type='image/jpeg')

 

WP-CLI is an EXTREMLY useful tool if you work with WordPress on the regular.