clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Change URL in WordPress database when migrating

Filed under: WordPress/PHP— Tagged with: deployment, snippets, database

Run these queries after migrating to make image paths point to new server.

2015.02.23 update added the find and replace technique and an external tool.

Few different methods to replace URLs in WordPress database. Handy when deploying.

Method 1: search and replace in a text editor

Just export the file, open in your favorite text editor and do a find and replace. This is not very 'scientific' and you need to do the importing and exporting process.

Method 2: with SQL queries

Here's few SQL queries to replace strings in WordPress database. I've always ran these in phpMyAdmin, choose the DB and then the SQL tab, paste these in one by one:

UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://www.old-domain.com', 'http://www.new-domain.com');

External tools

Dave in the comments links to this cool looking little tool: Search-Replace-DB.

Comments would go here, but the commenting system isn’t ready yet, sorry.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!