Here’s our complete guide to using WordPress WP-CLI, structured so you can use it professionally to manage, debug, and automate WordPress sites from the command line.


1. What is WP-CLI?

WP-CLI (WordPress Command Line Interface) is a command-line tool to manage WordPress installations without using a browser. You can:

  • Update WordPress core, plugins, and themes.
  • Manage users, posts, and options.
  • Import/export databases.
  • Run maintenance tasks.
  • Automate repetitive tasks with scripts.

Requirements:

  • SSH access to your server.
  • PHP CLI installed (php -v).
  • WordPress installed on your server.

Check WP-CLI installation:

wp --info

2. Installing WP-CLI

a) Using cURL:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp --info

Now you can run wp globally.

b) Verify:

wp --info
wp --version

3. WP-CLI Basics

a) Check WordPress Version:

wp core version

b) Update WordPress:

wp core update

c) Update Database after Core Update:

wp core update-db

d) Get WordPress Status:

wp core status

4. Managing Plugins

a) List Installed Plugins:

wp plugin list

b) Install a Plugin:

wp plugin install woocommerce --activate
  • --activate immediately activates the plugin.

c) Update All Plugins:

wp plugin update --all

d) Deactivate/Delete a Plugin:

wp plugin deactivate akismet
wp plugin delete akismet

5. Managing Themes

a) List Installed Themes:

wp theme list

b) Install and Activate Theme:

wp theme install twentytwentyone --activate

c) Update Themes:

wp theme update --all

6. Managing Users

a) List Users:

wp user list

b) Create User:

wp user create john john@example.com --role=editor --user_pass=StrongPassword123

c) Update User:

wp user update john --role=administrator

d) Delete User:

wp user delete john --reassign=admin
  • --reassign assigns all deleted user’s content to another user.

7. Managing Posts & Pages

a) List Posts:

wp post list --post_type=post

b) Create a Post:

wp post create --post_type=post --post_title="My WP-CLI Post" --post_status=publish

c) Update a Post:

wp post update 123 --post_title="Updated Title"

d) Delete a Post:

wp post delete 123 --force

8. Database Management

a) Export Database:

wp db export mydatabase.sql

b) Import Database:

wp db import mydatabase.sql

c) Optimize Database:

wp db optimize

d) Check Database:

wp db check

e) Search & Replace in Database:

wp search-replace 'http://oldsite.com' 'https://newsite.com' --all-tables

9. Maintenance & Debugging

a) Enable Debug Mode:

wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set WP_DEBUG_DISPLAY false --raw

b) Enable Maintenance Mode:

wp maintenance-mode activate
wp maintenance-mode deactivate

c) Clear Cache (if using caching plugin):

wp cache flush

10. Cron Jobs & Scheduled Tasks

  • List scheduled events:
wp cron event list
  • Run scheduled events immediately:
wp cron event run --due-now

11. Multisite Commands

  • List sites:
wp site list
  • Create a new site in network:
wp site create --slug=newsite --title="New Site" --email=admin@example.com
  • Delete a site:
wp site delete 2 --yes

12. WP-CLI Aliases & Shortcuts

  • Run commands in a specific site:
wp @staging plugin list
  • Use aliases for multiple environments (defined in wp-cli.yml):
@local:
  path: /var/www/html
@staging:
  url: https://staging.example.com
  • Example:
wp @staging plugin update --all

13. Tips & Best Practices

  • Always backup database before mass operations.
  • Use --dry-run for search-replace or delete commands.
  • Combine WP-CLI with scripts to automate updates, backups, and maintenance.
  • Use cron + WP-CLI for scheduled automated tasks (e.g., plugin updates, database backups).

14. Common WP-CLI Commands Summary

Task Command
Check WP version wp core version
Update WP core wp core update
List plugins wp plugin list
Install & activate plugin wp plugin install plugin-name --activate
Update all plugins wp plugin update --all
List themes wp theme list
Install & activate theme wp theme install theme-name --activate
Create user wp user create username email --role=role
Export database wp db export filename.sql
Import database wp db import filename.sql
Search & replace URLs wp search-replace old new
Enable maintenance mode wp maintenance-mode activate

WP-CLI is extremely powerful — it’s the developer’s ultimate shortcut to manage WordPress like a pro without touching the admin panel.


Need Expert WordPress Workflow Automation?

Managing WordPress at scale requires precision and automation. If you’re looking to streamline your development process or need high-level server management, NeedleCode is here to help. We provide:

  • Custom WP-CLI Integration: Automate complex tasks and site management.
  • Scalable WordPress Architecture: Design and deploy WordPress for high-traffic environments.
  • Advanced Site Migrations: Zero-downtime migrations and database optimizations.

Stop wasting time on manual updates. Contact NeedleCode Today for premium WordPress development and maintenance services tailored to your business needs.