brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: wp search-replace with export writes the changed WordPress database to a file

WordPress
~/tips/tip-wordpress-search-replace-export

wp search-replace 'https://old.example.com' 'https://new.example.com' --export=moved.sql

Makes a copy of the database with a new domain in it and leaves the live tables alone.

Moving a site to a new domain usually means: copy the database, then change the URLs in the copy. WP-CLI can do both in one step.

wp search-replace 'https://old.example.com' 'https://new.example.com' --export=moved.sql

With --export, nothing in the current database changes. The command reads every table, does the replacement in memory, and writes the result as SQL to moved.sql. Leave out the filename and the SQL goes to standard output, so you can pipe it into gzip. On the new server you import that file and the URLs are already right.

The nice part is that serialized PHP data is handled properly. A plain sed on a dump breaks serialized strings, because their stored length no longer matches. WP-CLI rewrites the lengths too.

I’d add --skip-columns=guid, because the guid column is meant to stay the same forever and feed readers use it. For huge tables, --export_insert_size changes how many rows go into each INSERT, 50 by default.

The export only contains the tables that search-replace looked at, so tables without the WordPress prefix are missing unless you add --all-tables. Check that the file has every table you need before you delete the old site.

Further reading: wp search-replace

More WordPress tips wp user application-password create anna backup-script --porcelain define( 'MEDIA_TRASH', true ); wp cli check-update wp theme delete --all wp media image-size All WordPress tips (85) All topics

Link