WordPress Search and Replace: URLs and Text Safely

Need to replace an old URL, domain, or repeated phrase across WordPress content? You can use a search-and-replace tool, WP-CLI, or a direct SQL query. The safest method depends on what you're changing: WP-CLI and reputable WordPress migration tools are generally better for site-wide changes because they can handle serialized data. A basic SQL REPLACE() query only changes the specific column and table you target.

This guide shows how to preview and replace text in post content with phpMyAdmin, explains the limitations of direct SQL, and includes a safer WP-CLI workflow. Always take a fresh database backup before changing production data.

If you have SSH or hosting-terminal access, WP-CLI's search-replace command is usually a better choice for a WordPress-wide URL migration because it understands PHP-serialized data. Start with a dry run so you can review the proposed changes without writing to the database.

# Preview matching changes without modifying the database
wp search-replace 'http://old.example' 'https://new.example' --dry-run

Review the output and confirm the source and destination strings are correct. After taking a backup and verifying the dry run, run the same command without --dry-run:

wp search-replace 'http://old.example' 'https://new.example'

Use your actual old and new values. Avoid broad replacements unless you understand every match. For a multisite installation or a migration involving specific tables, check the WP-CLI documentation and select the appropriate scope before running the live command.

Replace text in post content using phpMyAdmin

A direct SQL query can be useful for a narrow, well-understood change to the post_content column. It does not update every location in WordPress, and it should not be used as a universal site migration method.

  1. Make and verify a current database backup.
  2. Open phpMyAdmin from your hosting control panel and select the correct WordPress database.
  3. Confirm your actual WordPress table prefix. The examples below use wp_; your tables may use a different prefix.
  4. Run a read-only preview query first, replacing the sample strings with your exact values.
SELECT ID, post_title
FROM wp_posts
WHERE post_content LIKE '%http://old.example%';

Inspect the matching posts. If the results are exactly what you expect and your backup is ready, this query replaces the matching string only in post content:

UPDATE wp_posts
SET post_content = REPLACE(
    post_content,
    'http://old.example',
    'https://new.example'
)
WHERE post_content LIKE '%http://old.example%';

Replace wp_posts with your actual table name and use exact search and replacement strings. The WHERE clause limits the rows considered to content containing the old string. This query changes database content immediately; it is not a preview or an undoable WordPress editor action.

Important limitations of direct SQL

  • Only the selected column changes. This example targets wp_posts.post_content. It does not automatically update post metadata, options, widgets, plugin tables, or other fields.
  • Serialized data can break. Do not use a plain SQL replacement on PHP-serialized values, which may be stored in options or post metadata. A replacement can invalidate the stored string lengths. Use WP-CLI or a migration tool that supports serialized data.
  • Table prefixes vary. Do not assume the prefix is wp_; verify it first.
  • Backups are essential. A mistaken replacement can affect many records. Confirm the backup can be restored before running a bulk update.
  • Choose narrow search strings. Broad words or short URL fragments can change unrelated content.

Can a WordPress plugin do this?

Yes. A reputable search-and-replace plugin can provide a visual interface and, when it supports serialized data, is preferable to raw SQL for many site-wide updates. Test with a dry run first, check which tables are selected, and create a database backup before applying changes.

Frequently asked questions

Will this SQL query replace a URL everywhere on my site?

No. The example changes only matches in the post_content column of the selected posts table. Other tables and fields require separate handling. For a broad WordPress migration, use WP-CLI or a tool that understands serialized data.

Can I use SQL search-and-replace on post metadata or options?

Be careful. Metadata and options may contain PHP-serialized values. A basic SQL string replacement can corrupt those values. Use a serialized-data-aware tool instead.

Should I run a preview before replacing text?

Absolutely. Use a dry run with WP-CLI or a read-only SELECT query in phpMyAdmin, review the matches, and back up the database before any live update.

What if I only need to replace a URL in post content?

A targeted SQL query can work for that narrow case if you understand the table and column, use exact values, and have a verified backup. WP-CLI remains a good option when available.