WP-CLI Search Replace banner
s/r

WP-CLI
Search Replace

Fix WordPress URLs, migrations and mixed content from the command line

W

WP-CLI search-replace is one of the most useful WordPress command line tools. It lets you safely replace text inside the WordPress database, which is especially helpful after migrations, domain changes, staging-to-live moves and HTTPS upgrades.

The command is powerful, so this guide focuses on safe examples: dry runs first, database backups, mixed content fixes, and updating WordPress site URLs properly.

1. What does wp search-replace do?

wp search-replace searches your WordPress database for one value and replaces it with another.

It understands WordPress serialized data, which is one of the main reasons it is safer than running a raw SQL replace query manually.

Typical uses include:

  • Changing a site from one domain to another
  • Moving from staging to live
  • Fixing mixed content after enabling SSL
  • Replacing old URLs inside posts, pages, widgets and options
$ wp search-replace 'oldsite.com' 'newsite.com' --dry-run

+---------------------+---------+--------------+------+
| Table               | Column  | Replacements | Type |
+---------------------+---------+--------------+------+
| wp_options          | option_value | 12       | PHP  |
| wp_posts            | post_content | 148      | SQL  |
| wp_postmeta         | meta_value   | 34       | PHP  |
+---------------------+---------+--------------+------+

Success: 194 replacements to be made.

2. Always start with a backup

$ wp db export before-search-replace.sql
Success: Exported to 'before-search-replace.sql'.

Do this first

Search-replace changes database content. Before running the real command, export the database so you have a rollback point.

This matters even more on WooCommerce, membership, booking or busy production sites.

3. Use --dry-run before making changes

The safest first step is a dry run. This shows what would be changed without actually changing anything.

$ wp search-replace 'http://example.com' 'https://example.com' --dry-run

Example output:

+---------------------+----------------+--------------+------+
| Table               | Column         | Replacements | Type |
+---------------------+----------------+--------------+------+
| wp_options          | option_value   | 6            | PHP  |
| wp_posts            | post_content   | 93           | SQL  |
| wp_postmeta         | meta_value     | 17           | PHP  |
+---------------------+----------------+--------------+------+

Success: 116 replacements to be made.

If the number looks wildly wrong, stop and review the command before running it for real. The dry run is your “are you absolutely sure, chief?” moment.

4. Fix WordPress mixed content after enabling SSL

Mixed content usually happens when a site loads over HTTPS but still has old http:// URLs stored in the database. This can affect images, scripts, CSS files, internal links or embedded content.

$ wp db export before-https-fix.sql

Backup first

Export the database before replacing URLs.

$ wp search-replace 'http://example.com' 'https://example.com' --dry-run

Preview mixed content fix

Check how many replacements would be made.

$ wp search-replace 'http://example.com' 'https://example.com'

Apply the HTTPS replacement

Run only after checking the dry run output.

$ wp cache flush

Clear cache

Flush object cache so WordPress does not keep serving old values.

5. Update a WordPress site URL after migration

When moving a WordPress site to a new domain, you commonly need to update stored URLs inside the database.

$ wp db export before-domain-change.sql

$ wp search-replace 'https://oldsite.com' 'https://newsite.com' --dry-run

$ wp search-replace 'https://oldsite.com' 'https://newsite.com'

$ wp cache flush

This helps update URLs in posts, pages, widgets, metadata and options.

Tip

Use the exact old and new URLs where possible. If the old site used www, include it. If the old site used HTTPS, include that too.

6. Update home and siteurl options directly

For the main WordPress URL settings, you can also update options directly.

$ wp option get home
https://oldsite.com

$ wp option get siteurl
https://oldsite.com

$ wp option update home 'https://newsite.com'
Success: Updated 'home' option.

$ wp option update siteurl 'https://newsite.com'
Success: Updated 'siteurl' option.

This updates the main WordPress URL settings, but it does not replace old URLs inside posts, pages or metadata. For a full migration, you will usually still need wp search-replace.

7. Handle www and non-www changes

If you are changing from www to non-www, or the other way around, search-replace can help clean up stored URLs.

# www to non-www
wp search-replace 'https://www.example.com' 'https://example.com' --dry-run
wp search-replace 'https://www.example.com' 'https://example.com'

# non-www to www
wp search-replace 'https://example.com' 'https://www.example.com' --dry-run
wp search-replace 'https://example.com' 'https://www.example.com'

Make sure the chosen version matches your redirects, SSL certificate and WordPress settings.

8. Replace staging URLs with live URLs

When pushing a staging site live, you may need to replace staging URLs in the database.

$ wp db export before-staging-to-live.sql

$ wp search-replace 'https://staging.example.com' 'https://example.com' --dry-run

$ wp search-replace 'https://staging.example.com' 'https://example.com'

$ wp cache flush

This is one of the most common uses of WP-CLI during WordPress migrations.

9. Skip specific database tables

Sometimes you may want to avoid changing certain tables, such as logs, analytics, backups or custom plugin tables.

$ wp search-replace 'oldsite.com' 'newsite.com' --skip-tables=wp_actionscheduler_logs,wp_wfHits --dry-run

You can then run it without --dry-run once the output looks correct.

10. Export results to SQL instead of changing immediately

If you want to generate an SQL file rather than update the database immediately, use --export.

$ wp search-replace 'oldsite.com' 'newsite.com' --export=search-replace-output.sql

This can be useful for review, migration workflows or moving changes between environments.

Common examples

wp search-replace 'http://example.com' 'https://example.com' --dry-run

Preview mixed content fix

Shows how many HTTP URLs would be replaced with HTTPS.

wp search-replace 'http://example.com' 'https://example.com'

Apply mixed content fix

Replaces old HTTP URLs with HTTPS across the database.

wp search-replace 'https://oldsite.com' 'https://newsite.com' --dry-run

Preview domain migration

Checks replacements before changing old domain values.

wp option update home 'https://newsite.com'
wp option update siteurl 'https://newsite.com'

Update primary site URL settings

Updates the main WordPress URL options directly.

Common mistakes

  • Skipping the backup: always export the database first.
  • Skipping --dry-run: preview before changing data.
  • Using the wrong URL format: check whether the old value includes http, https, or www.
  • Forgetting cache: flush WordPress and server cache after changes.
  • Replacing too broadly: avoid replacing short generic strings that may appear in unrelated data.
  • Running from the wrong WordPress install: confirm your path before running the command.

Safe search-replace workflow

pwd
ls wp-config.php

wp db export before-search-replace.sql

wp search-replace 'old-value' 'new-value' --dry-run

wp search-replace 'old-value' 'new-value'

wp cache flush

This is the safest basic workflow: confirm location, backup, preview, apply, clear cache.

FAQ

Is WP-CLI search-replace safe?

It is safe when used carefully. It is designed to handle serialized WordPress data, but you should still back up the database and run --dry-run first.

How do I fix WordPress mixed content with WP-CLI?

wp db export before-https-fix.sql
wp search-replace 'http://example.com' 'https://example.com' --dry-run
wp search-replace 'http://example.com' 'https://example.com'
wp cache flush

How do I update a WordPress site URL with WP-CLI?

wp option update home 'https://newsite.com'
wp option update siteurl 'https://newsite.com'

Does search-replace update posts and pages?

Yes. It can update values inside posts, pages, metadata, options and other database tables.

Should I use SQL replace instead?

Usually no. WordPress stores some data in serialized format, and raw SQL replacements can break serialized values. WP-CLI is usually the safer option.

Safe workflow

Safe WP-CLI search-replace workflow

Always dry-run first, then export the database, then run the real replacement.

# 1. Preview what would change
wp search-replace 'http://old.example.com' 'https://new.example.com' --dry-run

# 2. Export the database before making changes
wp db export before-search-replace.sql

# 3. Run the replacement
wp search-replace 'http://old.example.com' 'https://new.example.com'

# 4. Flush cache if the site uses object cache
wp cache flush
WP-CLI search-replace is usually safer than raw SQL because it can handle serialized WordPress data.
FAQ

Frequently Asked Questions

What does wp search-replace do?

It searches the WordPress database for one value and replaces it with another, commonly after a migration or domain change.

Should I use --dry-run first?

Yes. --dry-run previews the changes without writing to the database.

Does WP-CLI search-replace handle serialized data?

Yes. That is one of the main reasons it is preferred over raw SQL replacement for WordPress.

Should I export the database before search-replace?

Yes. Always export the database before running the real replacement.

$ practise_next --topic wp-cli

Practise this next

Turn the guide into practice with a related quiz, builder, cheat sheet or learning path.