How to update all WordPress post content URLs with SQL

Updating all the URLs and resources in the WordPress site after migrating to a new domain or migrating to a domain from IP address is a painstaking task. Luckily, there is an easy way!

In this tutorial, I will show you the easy way to update all URLs thanks to SQL commands. The first thing you need is access to PhpMyAdmin tool or direct shell access to the database server. Once you have the database accesses, you can follow the below-mentioned steps to update all domains/IP address in your posts, comments, etc. to the new domain address.

PhpMyAdmin giving option for running SQL query
PhpMyAdmin giving option for running SQL query

Find and update all the posts with old domain in the contents

The following SQL query lists all the posts that contain the old domain. This is only for seeing the posts that have the old domain. 

SELECT * FROM wp_posts WHERE post_content LIKE '%YOUR_OLD_DOMAIN%'

Now, let’s replace all those old domain URLs with the new one with the following SQL command.

UPDATE wp_posts SET post_content = replace(post_content, 'YOUR_OLD_DOMAIN', 'YOUR_NEW_DOMAIN') 

Find and update all the comments with old domain

Similar to what we have done with post content, we can replace the old domain in the comments too.

The following SQL query lists all the comments that contain the old domain.

SELECT * FROM wp_comments WHERE comment_content LIKE '%YOUR_OLD_DOMAIN%'

Then to replace all those old domains in the comment, use the following SQL command

UPDATE wp_comments SET comment_content = replace(comment_content, 'YOUR_OLD_DOMAIN', 'YOUR_NEW_DOMAIN') 
WHERE comment_content LIKE '%YOUR_OLD_DOMAIN%'
This should make all the URLs use your new domain.
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.