Skip to content

WP-CLI commands that pay for themselves

The six WP-CLI commands that save us 1-3 hours per WordPress engagement, with the dollar value attached and the cron + bash glue we run them in.

WP-CLI has hundreds of commands. A handful of boring ones do most of the work on audits, migrations, restorations and monthly maintenance, and each of them saves an hour or more against doing the same job by hand in wp-admin or phpMyAdmin. Here they are, with the flags that make them safe.

1. wp search-replace with --dry-run and --export

The migration command, but used safely.

wp search-replace 'https://staging.example.com' 'https://example.com' 
  --dry-run --report-changed-only --skip-columns=guid

This command prevents more “we broke the site” moments than any other. Run it before every domain move, staging push and switch to HTTPS. The --dry-run flag prints what would change without writing anything. The --report-changed-only flag trims the output so it actually fits on a terminal. The --skip-columns=guid flag stops you from rewriting historical post GUIDs, which breaks feed readers and external links.

Once the dry-run looks clean, we rerun without --dry-run, then immediately run a content spot-check with wp post list --post_status=publish --format=csv plus a curl of the homepage to confirm nothing surprising landed.

Saves 1-2 hours per migration in not having to roll back, plus the customer-facing trust we keep by not breaking permalinks.

2. wp db query for surgical SQL when search-replace cannot reach

Some artefacts of an old install live in JSON blobs inside wp_options or in serialized PHP arrays inside theme settings. wp search-replace handles serialized data well, but it cannot fix everything (notably theme options with custom serialization, custom post-meta with embedded JSON references).

wp db query "SELECT option_name FROM wp_options 
  WHERE option_value LIKE '%old.example.com%' LIMIT 50;"

We use this as a sweep after every migration to find leftover references. It surfaces things wp search-replace glossed over. Pairs well with wp option get themename_options --format=json | jq . for inspection.

Saves 30-60 minutes per migration on hard-to-find leftover references, and prevents the “why is one widget still pointing at staging” support ticket two days later.

3. wp plugin list --format=json for retainer status snapshots

Every WordPress retainer client gets a monthly patch report. The data lives in WP-CLI, two minutes away:

wp plugin list --format=json --fields=name,status,version,update,update_version 
  > /tmp/plugin-state.json
wp theme list --format=json --fields=name,status,version,update,update_version 
  > /tmp/theme-state.json
wp core check-update --format=json > /tmp/core-state.json

A short script can turn these into a monthly patch report: what was updated, what could not be, and why. Built by hand, the same report takes an hour; from these files it takes minutes plus the human summary. Now it takes about 90 seconds plus the time to write the human summary.

Saves about 45 minutes per site per month.

4. wp eval-file for one-off fixes that should not be in code

Sometimes the right answer to “I need to backfill an ACF field on 3,000 posts” or “I need to flip user roles for half the editors” is a five-line PHP script, run once, deleted after. We do not want this in the theme, in a plugin, or in version control.

wp eval-file /tmp/backfill_field.php

The PHP file uses normal WP functions (get_posts, update_post_meta, etc.). We write it, dry-run it (print first, write second), then run it for real and delete the file.

This replaces the “let me ssh in and write a wp-admin tools-page that nobody else will ever use” anti-pattern. Saves 1-3 hours per data migration depending on complexity.

Important: always run wp db export before the eval-file, every time.

5. wp transient delete-all and wp cache flush

The single most useful support-ticket-killer command. Many “the site looks broken” reports are resolved by:

wp transient delete-all
wp cache flush
wp rewrite flush

Plus a curl with cache headers checked. If the issue persists past these three, then we start debugging. If it does not, we close the ticket and explain to the client what a transient is.

It is the first thing to run before deeper debugging. The clients still see this as “fast support”, which is a brand point we like.

6. wp media regenerate --yes --skip-delete

After every theme change, after every CDN switch, after every image-size addition in functions.php, this is what makes the site stop returning old thumbnail dimensions:

wp media regenerate --yes --skip-delete

The --yes flag is the “do not prompt” flag, which matters on a site with 8,000 attachments. The --skip-delete flag means we keep the previously generated thumbnails around in case the new ones turn out wrong, which is cheap insurance.

Saves about 45 minutes after each theme-change engagement, plus a follow-up ticket from a client noticing the wrong-size hero image two days later.

Three honorable mentions

wp cron event list and wp cron event run <hook>, for diagnosing why a scheduled job did not fire. WordPress cron is pseudocron and silently breaks under heavy traffic. This command tells you what is scheduled and lets you manually trigger.

wp user list --role=administrator --field=user_login, runs in our security audits as the first sweep for unfamiliar admin accounts. Followed by wp user list --role=administrator --fields=user_login,user_email,user_registered. Surfaces sketchy accounts in seconds.

wp post list --post_status=publish --meta_key=_some_field --format=count, for “how many posts have X tagged?” reporting, often used as the data input for ACF backfill scripts.

What we used to lean on, and stopped

wp config set for changing database credentials in wp-config.php, useful, but if you are touching production credentials, you should be writing them by hand because the audit trail matters.

wp option update siteurl and home, used to be our go-to before a migration. Now we let wp search-replace handle the whole pass instead, so the URL changes happen in one transactional sweep rather than two steps.

wp role create and wp cap add, once useful, now we generally manage roles via Members or User Role Editor in the admin so the configuration is visible and auditable, not buried in shell history.

Cron + bash glue is where this really pays off

Five of the six commands above end up inside a small bash script that runs from cron weekly. We push the plugin/theme/core state snapshot into a long-running log, then diff it week-over-week to catch silent version downgrades or unexpected reactivations. That diff is one of the most useful inputs to a security review, and it takes about twenty minutes of bash to set up.

A skeleton, run weekly as the site’s own user rather than as root, because root-owned files left behind by WP-CLI can break the site’s cache and uploads:

#!/usr/bin/env bash
set -euo pipefail
SITE_DIR="/home/example.com/public_html"
LOG_DIR="/var/log/wp-state/example.com"
mkdir -p "$LOG_DIR"

cd "$SITE_DIR"
wp plugin list --format=json --fields=name,status,version,update 
  > "$LOG_DIR/plugins-$(date +%F).json"
wp core check-update --format=json 
  > "$LOG_DIR/core-$(date +%F).json"
wp user list --role=administrator 
  --fields=user_login,user_email,user_registered --format=csv 
  > "$LOG_DIR/admins-$(date +%F).csv"

Put it in the site user’s crontab, for example to run every Monday at 03:00, and forget about it. The snapshots stay on the server, get rotated, and feed the next security review without any human effort.

Where this fits

The point of WP-CLI is not the long list of commands. It is the few you actually use every week, made boring on purpose, scripted, and quietly running while you do the harder work. The list above is what we rely on. If you want this set up properly, our WordPress care plans include the updates, backups and monthly reports this automates.

If you run a busy WordPress site and you are not using WP-CLI yet, the highest-leverage move is to install it on the server, get wp plugin list working from a cron, and start logging weekly state to a folder. The full setup takes about an hour. The payback shows up at the next emergency.

Common questions

What are the most useful WP-CLI commands?

wp search-replace with –dry-run for migrations, wp db query for leftovers it misses, wp plugin list and wp core check-update for patch reports, wp eval-file for one-off data fixes, wp transient delete-all with wp cache flush for odd behaviour, and wp media regenerate after image-size changes.

Is it safe to run wp search-replace on a live site?

Run it with –dry-run first, export the database before the real run, and skip the guid column. It handles serialized data correctly, which a plain SQL replace does not.

Should I run WP-CLI as root?

No. Run it as the user that owns the site, for example with sudo -u. Running as root can leave root-owned cache or upload files the site cannot overwrite afterwards.

How do I install WP-CLI?

Download the wp-cli.phar file from the official site, check it runs with php wp-cli.phar –info, make it executable and move it to /usr/local/bin/wp. Many hosting panels already include it.

Start here

Hit this one
yourself?

If any of the above is happening on your stack, send us the symptoms. We triage the same working day and quote before we start.

Which layer is on fire?
Your details stay with us. Always.