Do not ever use the guid element for creating links – ever – ever

I was recently (I always seem to start posts with that phrase) learning how to update a clients posts post_title, slug (aka post_name in the db) and guid when a post title was changed programatically. I was using get_sample_permalink() in combination with wp_update_post() to generate a permalink and update the post guid along with the other two fields I mentioned.

However, only post_title and post_name were updating.

After some research I realized my misunderstanding. While the guid looked like a permalink in my database it actually is not what’s being pulled from for links. At least by WordPress core files and methods.

I certainly was.

Ever see those post objects returned via WordPress $wpdb methods that look like this?

Yup, that guid element in thar object sure looks tasty and handy. Why would I use an extra function and db call like get_permalink() to find something that’s right in front of my face?

Well, as seen in the WordPress Codex:

The term “GUID” stands for “Globally Unique Identifier”. It is a field that is intended to hold an identifier for the post which a) is unique across the whole of space and time and b) never, ever changes. The GUID field is primarily used to create the WordPress feeds.

When a feed-reader is reading feeds, it uses the contents of the GUID field to know whether or not it has displayed a particular item before. It does this in one of various ways, but the most common method is simply to store a list of GUID’s that it has already displayed and “marked as read” or similar.

Thus, changing the GUID will mean that many feedreaders will suddenly display your content in the user’s reader again as if it was new content, possibly annoying your users.

And furthermore, we use Search-Replace-DB to update database url’s when we move sites from domain to domain. At a point in the process one of the forms asks “Leave GUID column unchanged?” which of course I never checked.

So, sorry for all those people who possibly got extra old feeds in their feed reader.

http://codex.wordpress.org/Changing_The_Site_URL#Important_GUID_Note