Quickly update WordPress links using SQL Script

So you’ve migrated your WordPress site to another server and have found your links are all pointing to the old site.  Sure, you can go through and manually update every.single.link, but there’s a much easier way.

When I mention to people that it’s just a quick script to run in SQL, I can see them mentally running for the hills. Bear with me, it’s nothing to be afraid of.

This post will take you step by step through the process of running a SQL script that will update your URLs in no time.  Untie your runners, you won’t be needing them.  This is a very simple process, I promise!

As always, I recommend taking a full backup of your site before you proceed with any updates or changes on your site.

Step 1: Opening PHP Admin

To run a SQL script on your WordPress database, you will need to open PHPAdmin.  This is an application that you can find in your cPanel interface.

Once you have opened PHPAdmin, click onto your database name in the left-hand menu.

Tip. If you have multiple databases installed, you can click on the wp_options table (or wpdx_options in my instance).  Take a look at the siteurl column and it will tell you which site this database relates to. See the example below

Step 2: The script

Copy and paste the following script into the SQL Query box:

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

 This is the part that gets a little more complicated, but it’s not difficult.

You will see there are three lines to this script.  Each line updates certain fields within the database, so each will need to be edited to reflect the changes you want to make on the site.

There are two parts of each line of the script you need to focus on, the table names and the URLs.

  1. Table names
  2. URLs

Table names

In the script you will see references to wp_posts (used in lines 1 & 2) and wp_postmeta (used in line 3).  You need to update the script to the names of your own table prefixes.

For example, in my case, the beginning of my tables is ‘wpdx_‘.  If you’re not sure what your table prefix is, take a look at the list on the left

To edit, simply type over the ‘wp_‘ with the name of your own table prefix.  You only need to edit the prefix.



Next, you need to replace the URLs with your own.  Replace the http://www.oldurl.com with the URL that your site is currently pointing to (the incorrect URL).  You only need to replace the beginning of the URL, don’t place any extra characters.  Make sure you replace it on all three lines.

Once the old URL has been updated, it’s time to update the http://www.newurl.com with the URL of your new site (the correct URL).


Take extra care not to add or remove any characters that exist in the script, for example quote marks, commas and brackets. 

Only use the direct URLs, not the full links, for example

  • Correct: http://webhustle.com.au  (don’t add anything extra characters after the URL)
  • Incorrect: http://webhustle.com.au/

If you are unsure, see my example below. 

Once you’re happy with the edits, you can click Go to run the script.

If you get an error…

The script will not run. Go back over the scripts to see what might be wrong.  The error will often let you know what it not working.

If you make a mistake…

Don’t stress. Simply repeat the process to replace the links with the correct ones.

Step 3: The last step

Once you’ve successfully run the SQL script, go back to the admin section of your web site.

 Head to Settings > Permalinks and click save.  This is the final step towards indexing your site with the new URLs.

 After that, go back into your site and refresh the screen.  Your URLs should now be pointing to the correct spot!

Still stuck?

Get in touch via the Web Hustle Community on Facebook and we’ll get you on the right track!

Meg on the couch with the Web Hustle Beagles

About Web Hustle

Web Hustle helps small businesses like yours!

Learn web design using our online courses, tutorials, master classes and downloadable eBooks, so you can offer web design services to your own clients.

Build the skills and confidence you need to create the flexible life you deserve!

Humm - Buy it today, take time to pay