WP-CLI commands let you manage WordPress from the terminal: export and import the database, update core, plugins and themes, run search-replace, manage users, regenerate images, flush caches, run cron events and manage WooCommerce data. The commands below are the ones I use most, with the exact syntax and the safety habits around them.
This is part of my automation series.
What is WP-CLI and why use it?
WP-CLI is the official command-line tool for WordPress. It is faster than clicking through the admin, works when the admin is broken, and makes tasks repeatable in scripts. On slow servers it also avoids web timeouts for long jobs, such as regenerating thousands of thumbnails.
Check it is available:
wp --info
wp core version
Run commands from the WordPress root, or add --path=/path/to/site.
Backups and the database
# Export before anything risky
wp db export ~/backups/site-$(date +%F).sql
# Import (overwrites the database!)
wp db import ~/backups/site-2026-10-01.sql
# Database size and tables
wp db size --tables
# Optimise tables
wp db optimize
Rule I never break: export first, then change.
Updates
wp core check-update
wp plugin list --update=available
wp plugin update --all --dry-run
wp plugin update woocommerce
wp theme update --all
wp core update && wp core update-db
For stores, I update major plugins one at a time on staging first. Combined with a plugin audit, this keeps sites stable.
Search and replace
The classic use: moving a site to a new domain or from HTTP to HTTPS. WP-CLI handles serialised data correctly, unlike a raw SQL replace.
wp search-replace 'http://old.example.com' 'https://example.com' --all-tables --dry-run
wp search-replace 'http://old.example.com' 'https://example.com' --all-tables --precise
Read the dry-run output before running it for real. Useful for migrations.
Users
wp user list --role=administrator --fields=ID,user_login,user_registered
wp user create jane [email protected] --role=editor
wp user update 12 --role=subscriber
wp user application-password list 1
Reviewing administrators regularly is part of my security checklist.
Content and options
wp post list --post_type=page --fields=ID,post_title,post_status
wp post create --post_type=post --post_title='Draft title' --post_status=draft
wp post meta get 123 _thumbnail_id
wp option get permalink_structure
wp option update blog_public 1 # make sure search engines are not discouraged
wp rewrite flush
Media
wp media regenerate --only-missing --yes
wp media import ~/images/*.jpg --post_id=123
wp media image-size
--only-missing is gentle on slow servers. See image optimisation.
Cache and transients
wp cache flush # object cache
wp transient delete --expired
wp transient delete --all
Page cache plugins usually register their own commands; check wp help to see what is installed.
Cron
wp cron event list
wp cron event run --due-now
wp cron test
Details in the WP-Cron guide.
WooCommerce
WooCommerce adds commands under wp wc, mapping to its REST API. Most need a user:
wp wc product list --user=1 --fields=id,name,sku,stock_quantity
wp wc order list --user=1 --status=processing
wp wc webhook list --user=1
wp wc tool run clear_transients --user=1
Related: WooCommerce webhooks and CSV product imports.
Running PHP
wp eval 'echo get_bloginfo("name");'
wp eval-file scripts/fix-meta.php
wp shell
eval-file is my preferred way to run one-off data fixes: the script is reviewed, versioned and repeatable.
Writing a custom command
When I repeat the same steps, I make a command. In a plugin:
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'mx stale-drafts', function ( $args, $assoc ) {
$days = isset( $assoc['days'] ) ? absint( $assoc['days'] ) : 90;
$posts = get_posts( array(
'post_status' => 'draft',
'numberposts' => -1,
'date_query' => array( array( 'before' => "$days days ago" ) ),
) );
foreach ( $posts as $p ) {
WP_CLI::log( sprintf( '%d %s', $p->ID, $p->post_title ) );
}
WP_CLI::success( count( $posts ) . ' stale drafts.' );
} );
}
Run with wp mx stale-drafts --days=60. Custom commands are also safer for AI-assisted work: one reviewed command is easier to trust than many ad-hoc changes.
Running remotely
WP-CLI supports SSH aliases in wp-cli.yml:
@prod:
ssh: [email protected]:22/home/user/public_html
Then wp @prod plugin list runs on the server from your machine.
Safety habits
- Export the database before data-changing commands.
- Use
--dry-runwhen available. - Test on staging first.
- Run heavy jobs off-peak on slow hosting.
- Keep scripts in version control.
Common mistakes
- Running search-replace without a dry run.
- Running commands as root, creating files the web server cannot write.
- Forgetting
--userfor WooCommerce commands. - Running from the wrong directory and changing another site.
If you want routine WordPress work scripted for your team, contact me.
Frequently asked questions
What is WP-CLI?
WP-CLI is the official command-line interface for WordPress. It lets you manage plugins, themes, users, content, the database and settings from a terminal without using the admin dashboard.
How do I clear the cache with WP-CLI?
wp cache flush clears the WordPress object cache. Page caching plugins usually provide their own commands, and wp transient delete --all clears transients.
Is WP-CLI available on shared hosting?
Many hosts include it over SSH. Check your host's documentation; if not installed, it can often be added as a single phar file in your home directory.
Can WP-CLI break my site?
Commands that change data, such as search-replace or plugin updates, can. Take a database export first, use --dry-run when offered, and test on staging.


