Skip to content

WordPress Search & Replace SQL Generator

Change your site URL without corrupting serialized data

Runs in your browser. Nothing is uploaded.

The change

Read this before you run anything

WordPress stores many options as PHP-serialized strings, and a serialized string records its own byte length: s:19:"http://old-site.com". A plain SQL replace changes the text but not the length, so PHP can no longer unserialize the row. Nothing errors; WordPress just gets back false instead of your settings.

What you lose: any widget settings, theme mods (Customizer settings) and plugin settings that contain the old URL. Nothing reports an error, so the damage is easy to miss.

This is why the queries below touch only two rows of wp_options, home and siteurl, which are stored as plain text.

What are you moving?
search-replace.sql

Take a database backup first. Not 'consider taking one' — take one. There is no undo for an UPDATE.

About the WordPress Search & Replace SQL Generator

A raw SQL replace breaks serialized WordPress data when the new URL has a different length. This writes the safe URL-change queries and the WP-CLI command.

Enter the old and the new site address. The tool writes the two UPDATE queries that are safe as plain SQL, and the WP-CLI command for everything else. It will not write a live query for rows that may hold serialized data: if you tick post meta, that query comes out commented out, and the output says why. For the whole move, including the plugin route, read the guide to search and replace in the WordPress database.

Worked example: moving a site from staging to live

Input. Only "Post content and excerpts" is ticked, which is the default.

Old URL:       https://staging.example.com
New URL:       https://example.com
Table prefix:  wp_

Output, from running the tool's own script in Node.js with that input on 16 November 2026.

-- Generated at odiethemes.com/tools/wordpress-search-replace-sql
--
-- TAKE A DATABASE BACKUP BEFORE RUNNING THIS. There is no undo.
--
-- Replacing: https://staging.example.com
--      with: https://example.com

-- 1. The two rows in wp_options that are NOT serialized, and so are safe to
--    update directly. These are the only wp_options rows we touch.
UPDATE `wp_options`
SET option_value = REPLACE(option_value, 'https://staging.example.com', 'https://example.com')
WHERE option_name IN ('home', 'siteurl');

-- 2. Post content and excerpts. Plain text columns. Safe.
UPDATE `wp_posts`
SET post_content = REPLACE(post_content, 'https://staging.example.com', 'https://example.com'),
    post_excerpt = REPLACE(post_excerpt, 'https://staging.example.com', 'https://example.com');

-- ─────────────────────────────────────────────────────────────────────
-- WHAT TO DO INSTEAD, for everything above that we refused to emit:
--
-- WP-CLI understands PHP serialization and will rewrite the length prefixes
-- correctly. It handles options, postmeta, usermeta — everything:
--
--   wp search-replace 'https://staging.example.com' 'https://example.com' --all-tables-with-prefix --skip-columns=guid --dry-run
--   wp search-replace 'https://staging.example.com' 'https://example.com' --all-tables-with-prefix --skip-columns=guid
--
-- Run the --dry-run first. It shows a report and saves nothing.
-- --all-tables-with-prefix also covers plugin tables that share the prefix,
-- without touching other apps in the same database (--all-tables would).
-- Multisite: read the --network and --url options in the WP-CLI docs first;
-- they decide which sites' tables are covered.
--
-- No WP-CLI access? The Better Search Replace plugin does the same thing from
-- the admin, and is serialization-aware. Either is correct. Raw SQL is not.
-- ─────────────────────────────────────────────────────────────────────

What each part does:

  • Lines starting with -- are SQL comments, so you can paste the whole output into phpMyAdmin or the mysql client.
  • Query 1 changes two rows of wp_options: home and siteurl, the Site Address and WordPress Address from Settings > General. Both hold a plain URL, so a text replace is safe.
  • Query 2 changes links and image addresses inside posts, pages and their excerpts. Those columns are plain text. Blocks store their settings in the same text, and WordPress writes them as JSON with unescaped slashes, so a normal URL matches there too (read from the WordPress 7.1.4 source).
  • Nothing else is touched: no other wp_options rows, no post meta, no user meta and no GUIDs. Widgets, Customizer settings and most plugin settings live in those places, often as serialized data.
  • The two WP-CLI lines do the rest. --dry-run runs the whole operation and reports how many replacements it would make, without saving. --all-tables-with-prefix includes plugin tables that start with wp_, and --skip-columns=guid leaves the post GUIDs alone, as the WordPress migration guide asks.

Why it will not write the other queries

WordPress saves arrays, such as widget and plugin settings, as PHP-serialized text, and that text records the length of each string it holds. A plain SQL replace changes the text but leaves the recorded length as it was.

In our test on WordPress 7.1.4 (PHP 8.3, WordPress Playground, November 2026), we saved an option holding the array ['logo' => 'http://example.com'] and ran REPLACE(option_value, 'http://', 'https://') on it in SQL. Playground keeps its database in SQLite, so SQLite ran the query, not MySQL; the length prefix is written by PHP, not by the database. The stored text became a:1:{s:4:"logo";s:18:"https://example.com";}: a 19-character URL still marked as 18. get_option() then returned false, with WP_DEBUG and WP_DEBUG_LOG on and nothing written to debug.log. A plain-text option changed in the same query read back fine. Nothing warns you, so the lost setting is easy to miss.

When to use it

Use the SQL when you can only reach the database, for example through phpMyAdmin, and the site address plus the links in posts are all you need to change. For a full move, use WP-CLI or the Better Search Replace plugin: both rewrite serialized data correctly.

The same address can appear in more than one form. If your old content mixes http:// and https://, or www and no www, run the tool once for each old form. After an HTTPS move, the guide to fixing WordPress mixed content shows how to find the http:// URLs still left in the database.

Limits

  • MySQL's REPLACE() is case-sensitive, so HTTPS://Staging.Example.com would not change.
  • Some plugins store URLs as JSON with escaped slashes, such as https:\/\/staging.example.com, often in post meta. A search for the normal URL does not match that text, so this SQL leaves it alone, and so does a plain wp search-replace run. A second pass with the escaped form does match. We checked the string match itself in Node.js on 6 November 2026, not inside WordPress or WP-CLI.
  • On a multisite network, each extra site has its own tables (wp_2_options, wp_2_posts and so on), and the network's site addresses are stored in wp_blogs and wp_site. These queries cover only the tables of the prefix you enter.
  • The WP-CLI lines were checked against the WP-CLI documentation. We did not run wp search-replace for this page.

Sources

Guides for this tool

FAQ

Questions

Can I change my WordPress site URL with a SQL find and replace?

Not safely. Many options are stored as PHP-serialized strings that record their own length. A plain SQL replace changes the text but not the length, so PHP can no longer unserialize the row. In our test on WordPress 7.1.4 and PHP 8.3 (WordPress Playground, which keeps its database in SQLite, November 2026), replacing http:// with https:// this way made get_option() return false for a serialized setting, while a plain-text option came through fine.

What should I use instead?

WP-CLI: run wp search-replace 'old' 'new' --all-tables-with-prefix --skip-columns=guid --dry-run first, then the same command without --dry-run. It understands serialization and rewrites the length prefixes. Without command-line access, the Better Search Replace plugin does the same job from wp-admin.

Should I update the GUID column?

No. The GUID is a permanent unique identifier for a post, not a link, and feed readers key off it, so rewriting it can make them show old posts again as new. WordPress's own guide to migrating a site says never to change it. That is why the WP-CLI command above skips the guid column.