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?

[0] => WP_Post Object
        (
            [ID] => 1348
            [post_author] => 296
            [post_date] => 2015-02-19 18:19:33
            [post_date_gmt] => 2015-02-19 23:19:33
            [post_content] => 
            [post_title] => A new article 16
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => closed
            [post_password] => 
            [post_name] => a-new-article-16
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2015-02-19 19:41:33
            [post_modified_gmt] => 2015-02-20 00:41:33
            [post_content_filtered] => 
            [post_parent] => 1347
            [guid] => http://dev.example.com/essays_for_comment/a-new-article-33/a-new-article-33/
            [menu_order] => 0
            [post_type] => essays_for_comment
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)

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

Leave a Reply

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