Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

WordPress Change URL – MySQL – Update String Within Column

Posted on October 17, 2014January 15, 2021 By David Kittell

Recently I changed a domain alias for a WordPress site and needed to change the domain in all the pages/posts and this little script came in handy.

Typical disclaimer stands, do a full backup before running this and verify you have everything setup correctly.

UPDATE wp_options SET option_value = REPLACE(option_value, 'wp.domain.com', 'www.domain.com') WHERE option_value LIKE ('%wp.domain.com%');
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, 'wp.domain.com', 'www.domain.com') WHERE meta_value LIKE ('%wp.domain.com%');
UPDATE wp_posts SET post_content = REPLACE(post_content, 'wp.domain.com', 'www.domain.com') WHERE post_content LIKE ('%wp.domain.com%');
UPDATE wp_posts SET guid= REPLACE(guid, 'wp.domain.com', 'www.domain.com') WHERE guid LIKE ('%wp.domain.com%');

If you have a network site first update your wp-config.php

sed -i 's|wp.domain.com|www.domain.com|g' wp-config.php

Secondly update all database tables, note this may be different depending on how many sites you have.

Update wp_site SET domain = 'www.domain.com' WHERE domain = 'wp.domain.com';
Update wp_sitemeta SET meta_value = 'http://www.domain.com/' WHERE meta_key = 'siteurl';
Update wp_sitemeta SET meta_value = 'www.domain.com' WHERE meta_key = 'dm_ipaddress';
UPDATE wp_blogs SET domain = 'www.domain.com' WHERE domain = 'wp.domain.com' ;
UPDATE wp_options SET option_value = REPLACE(option_value, 'wp.domain.com', 'www.domain.com') WHERE option_value LIKE ('%wp.domain.com%');
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, 'wp.domain.com', 'www.domain.com') WHERE meta_value LIKE ('%wp.domain.com%');
UPDATE wp_posts SET post_content = REPLACE(post_content, 'wp.domain.com', 'www.domain.com') WHERE post_content LIKE ('%wp.domain.com%');
UPDATE wp_posts SET guid= REPLACE(guid, 'wp.domain.com', 'www.domain.com') WHERE guid LIKE ('%wp.domain.com%');
Update wp_usermeta SET meta_value = 'www.domain.com' WHERE meta_key = 'source_domain';

UPDATE wp_2_options SET option_value = REPLACE(option_value, 'wp.domain.com', 'www.domain.com') WHERE option_value LIKE ('%wp.domain.com%');
UPDATE wp_3_options SET option_value = REPLACE(option_value, 'wp.domain.com', 'www.domain.com') WHERE option_value LIKE ('%wp.domain.com%');
UPDATE wp_4_options SET option_value = REPLACE(option_value, 'wp.domain.com', 'www.domain.com') WHERE option_value LIKE ('%wp.domain.com%');
UPDATE wp_5_options SET option_value = REPLACE(option_value, 'wp.domain.com', 'www.domain.com') WHERE option_value LIKE ('%wp.domain.com%');
UPDATE wp_6_options SET option_value = REPLACE(option_value, 'wp.domain.com', 'www.domain.com') WHERE option_value LIKE ('%wp.domain.com%');

UPDATE wp_2_postmeta SET meta_value = REPLACE(meta_value, 'wp.domain.com', 'www.domain.com') WHERE meta_value LIKE ('%wp.domain.com%');
UPDATE wp_3_postmeta SET meta_value = REPLACE(meta_value, 'wp.domain.com', 'www.domain.com') WHERE meta_value LIKE ('%wp.domain.com%');
UPDATE wp_4_postmeta SET meta_value = REPLACE(meta_value, 'wp.domain.com', 'www.domain.com') WHERE meta_value LIKE ('%wp.domain.com%');
UPDATE wp_5_postmeta SET meta_value = REPLACE(meta_value, 'wp.domain.com', 'www.domain.com') WHERE meta_value LIKE ('%wp.domain.com%');
UPDATE wp_6_postmeta SET meta_value = REPLACE(meta_value, 'wp.domain.com', 'www.domain.com') WHERE meta_value LIKE ('%wp.domain.com%');

UPDATE wp_2_posts SET post_content = REPLACE(post_content, 'wp.domain.com', 'www.domain.com') WHERE post_content LIKE ('%wp.domain.com%');
UPDATE wp_3_posts SET post_content = REPLACE(post_content, 'wp.domain.com', 'www.domain.com') WHERE post_content LIKE ('%wp.domain.com%');
UPDATE wp_4_posts SET post_content = REPLACE(post_content, 'wp.domain.com', 'www.domain.com') WHERE post_content LIKE ('%wp.domain.com%');
UPDATE wp_5_posts SET post_content = REPLACE(post_content, 'wp.domain.com', 'www.domain.com') WHERE post_content LIKE ('%wp.domain.com%');
UPDATE wp_6_posts SET post_content = REPLACE(post_content, 'wp.domain.com', 'www.domain.com') WHERE post_content LIKE ('%wp.domain.com%');

UPDATE wp_2_posts SET guid= REPLACE(guid, 'wp.domain.com', 'www.domain.com') WHERE guid LIKE ('%wp.domain.com%');
UPDATE wp_3_posts SET guid= REPLACE(guid, 'wp.domain.com', 'www.domain.com') WHERE guid LIKE ('%wp.domain.com%');
UPDATE wp_4_posts SET guid= REPLACE(guid, 'wp.domain.com', 'www.domain.com') WHERE guid LIKE ('%wp.domain.com%');
UPDATE wp_5_posts SET guid= REPLACE(guid, 'wp.domain.com', 'www.domain.com') WHERE guid LIKE ('%wp.domain.com%');
UPDATE wp_6_posts SET guid= REPLACE(guid, 'wp.domain.com', 'www.domain.com') WHERE guid LIKE ('%wp.domain.com%');

Remember sometimes the domain is not in the database, if needed you can do this command to search code files

grep -R 'wp.domain.com' /var/www/html
Originally Posted on October 17, 2014
Last Updated on January 15, 2021
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MySQL WordPress WordPress SQL

Post navigation

Previous post
Next post

Related Posts

Windows CMD – Disk Partition and Format with DiskPart

Posted on December 22, 2016December 22, 2016

NOTE: This is not put in code as it can be VERY difficult reverse the process below. ONLY do this if you have a clue of what you are doing . This process has been tested on a USB Hard Drive, Thumb Drive, and Physical (Internal to computer) Hard Drive…

Read More

PHP CLI – Create WordPress Username

Posted on October 29, 2015February 26, 2016

Currently this code only creates a subscriber user in WordPress. Due to security concerns I likely won’t work on this to make it do greater permissions. php wp-create-user.php MyBasicUser User@example.com # php wp-create-user.php MyBasicUser User@example.com Username not in use, creating user. User Created Username: MyBasicUser Password: 5y1kZSkMO0bP # # php…

Read More

Ektron Content Type

Posted on October 21, 2013October 26, 2015

Public Function GetContentType(ByVal sId As String) As String Select Case [sId] Case "1" Return "Content" Case "2" Return "Forms" Case "3" Return "Archive Content" Case "4" Return "Archive Forms" Case "7" Return "Library Item" Case "8" Return "Assets" Case "9" Return "Archive Assets" Case "12" Return "Archive Media" Case "99"…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • Front Page
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes