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.
Once you’ve found the correct database, click SQL from the tabs at the top.
Step 2: The script
Copy and paste the following script into the SQL Query box:
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.
- 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: https://webhustle.com.au (don’t add anything else after this)
- Incorrect: https://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!