I sent a code snippet earlier today and realized. Duh. I should be blogging this.

Here is some code that I have used to update the guid in my wp_posts table. Replace ‘example.com’ with whatever your base url is for your WordPress installation.

Two update queries, one for posts and one for pages. This only updates published posts and ones which don’t have the guid set yet (remove the guid = ” line to replace existing guid values).

update wp_posts
set guid = concat('http://www.example.com/?p=',ID)
where guid = ''
and post_status = 'publish'
and post_type = 'post';

update wp_posts
set guid = concat('http://www.example.com/?post_id=',ID)
where guid = ''
and post_status = 'publish'
and post_type = 'page';

Update: Also Change Your Site URL in Posts in SQL

I have recently been using a different method when moving sites to update the guid and also to do a search and replace within posts and pages for the url of your site. This is necessary when changing your site url since uploaded image references in your posts are set to the absolute url.

/* Use this to update the guid url */
UPDATE wp_posts SET guid = REPLACE (
guid,
'http://www.example-old-url.com',
'http://www.example-new-url.com');  

/* Use this to update urls in posts, good for images! */
UPDATE wp_posts SET post_content = REPLACE (
post_content,
'http://www.example-old-url.com',
'http://www.example-new-url.com');

Tags: , ,

8 Responses to “Updating guid in Your WordPress Database”

  1. kaigou 29. Apr, 2009 at 9:18 am #

    You freaking ROCK. You just saved me hours and hours of madness after moving posts from one database to a new one — while on the same domain, which meant I couldn’t use existing plugins for domain db transfers. One simple query and tadah, all straightened out. I shall now dance around my office in glee at having twenty spare minutes, in joyous gratitude! Thank you for this!

    • Rick Tuttle 29. Apr, 2009 at 9:45 am #

      I’m glad I could help kaigo. I just updated this post with more info on updating your url in the database by doing a search and replace in mySQL. I’ve found that method to be very handly.

  2. Matt Di Pasquale 10. Jun, 2009 at 9:48 am #

    I could be wrong, but shouldn’t both the old and new URLs both either have the slash or not… like this:
    ‘http://www.example-old-url.com’,
    ‘http://www.example-new-url.com’);

    instead of this:
    ‘http://www.example-old-url.com/’,
    ‘http://www.example-new-url.com’);

    otherwise, that slash will get erased, and you could get a URL like:
    http://www.example-new-url.comblog/
    when you really want:
    http://www.example-new-url.com/blog/

    • Rick Tuttle 10. Jun, 2009 at 10:14 am #

      You are absolutely correct Matt. I have fixed the typo in the code.

  3. Clive 23. Jul, 2009 at 7:45 am #

    Hi there, thank you so much for this. help me bigtime!

    One comment, your inital code block said

    set guid = concat(‘http://www.example.com/?post_id=’,ID)

    whereas maybe it should be

    set guid = concat(‘http://www.example.com/?paget_id=’,ID)

    Thanks again though!

  4. Clive 23. Jul, 2009 at 7:46 am #

    Sorry for my typo!

    set guid = concat(‘http://www.example.com/?page_id=’,ID)

  5. Gell 15. Dec, 2009 at 4:32 am #

    Where can you add this code in the source files of WordPress so that all new messages have the correct guid-link in the wp_post table?

  6. Christian 19. Jan, 2010 at 4:18 pm #

    Thanks a lot – saved me from all kinds of hell…