Don’t use the guid field. Ever. Ever, ever.
What is a GUID?
GUID stands for “Globally Unique IDentifier”. The important property of a GUID is that it is unique; it matches no other GUID now or in the future. As a unique identifier it should never change.
What does WordPress use the ‘guid’ fields in the database for?
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.
Feed readers need to keep track of which posts have been read and which posts are new. A GUID is the perfect solution for this: it is unique for every post and should never change.
So why is the guid the same as an image/file attachment’s URL?
In the beginning it was probably reasoned that the URL was a convenient and easy unique identifier. In hindsight, it might be argued that using a UUID formatted hash would have caused less confusion over its purpose. Regardless, we should really consider it purely coincidence that the guid field matches an image attachment file’s original URL. In fact, if you change the domain, subdomain, move WordPress to a sub-directory, out of a sub-directory– anything that would change the URL to an image attachment’s source file after it has been uploaded to the Media Library– then the guid field will NOT match the source file’s URL. The guid field cannot and will not be updated by WordPress because it is designed to be used as a unique identifier for an item in wp_posts and outside tools may be using the original guid.
Can’t I just update the guids in the database?
You can, and it’s probably no harm for Media Library attachments, but it’s not necessary and you still run the risk of images/attachments breaking again in the future. For some time now, WordPress has stored the attachment file name as part of the attachment’s metadata and it can always assemble the correct URL for an attachment using that in conjunction with the current settings. The WordPress Codex has several functions to do this for you:
And Pods has a few handy wrappers for images, as well:
- The Pods field() function also has some special traversal options to handle URL output for attachments
Big deal, my URLs will never change
Maybe. But websites are living, breathing things and there are plenty of reasons why it may change. You might start a site with a development URL using a subdomain and then move it for production (eg dev.example.com -> www.example.com). You might have a hosting account that provides a preview URL for you that won’t match when you go live (eg somehosting.example.com/~username/). You may find that reallycooldomainname.com becomes available a year from now, snatch it up, and move to that domain name. Getting the URL the right way isn’t difficult and it can save you or those who come after you time and trouble.