Updating attachments guid in the WordPress database

This is something that happens when it’s time to move from development to production with a WordPress website, or more generally when you change the domain name of a WordPress website. The path of attachments – pictures, media, documents – which is also their guid, should change accordingly. Otherwise, if in some places your theme displays post attachments by referring to their guid, the path will be wrong and, in the case of pictures for example, these will not be displayed if they were originally on a localhost or if, say, you get rid of site on the old domain altogether.

Hopefully there is a one minute formula to avoid this: a one line SQL statement that will update all guids of attachments in one go, directly in the WordPress database.

The procedure

Access your new WordPress database via phpMyAdmin.

Click the ‘SQL’ tab – framed in red in the picture below.

SQL tab in phpMyAdmin

In the ‘Run SQL query/queries on database nameofdatabase‘ field, enter the following query:

UPDATE `pre_posts` SET `guid` =  REPLACE( `guid` , 'http://old-url-to-wordpress' , 'http://new-url-to-wordpress' ) WHERE `post_type` = 'attachment'

pre_ is your table prefix (the default being ‘wp_’)

http://old-url-to-wordpress is the previous url to the WordPress directory (not to mix up with the home url!)

Similarly, http://new-url-to-wordpress is the new url to the WordPress directory.

And this should do the trick!

Like that blog post? Get more straight to your inbox PLUS... your FREE checklist to ensure your websites are developed on budget and on time.

Leave a Comment

Your email address will not be published. Required fields are marked *