0

I understand that guid is a unique reference for a post, whether that post is a post or an attachment.

When it's a post, I think guid is self-evident. But what is the correct formulation/destination for that URL when it is an attachment?

Should it point to the image file itself, or to the "post" page that WordPress stands up to render images?

If the latter, should the URL contain the slug of the post_parent (post to which it the image is attached), or not?

I am seeing a few different examples/options...

  • http://www.domain.com/wp-content/uploads/YYYY/MM/image.jpeg (esp. if not attached?)
  • http://www.domain.com/YYYY/MM/slug-of-the-post/slug-of-the-image/ (esp. if attached?)

What is the value in it including the post_parent slug? This may be unnecessary?

If it's all personal choice as long as it's unique, well, what is WordPress' default behaviour?

Question not answered by similar:

6
  • I wondered what makes you asked this question? Are you manually creating the attachment post, e.g. you upload the image using wp_handle_upload() and then manually call wp_insert_post()? Because core functions like media_handle_upload() and media_handle_sideload() will upload the media file (like an image) and create an attachment post for that media, so you don't need to worry about manually setting the GUID. And those core functions, including the REST API, default to using the file URL, which may look like https://example.com/wp-content/uploads/2023/02/image.jpg.
    – Sally CJ
    Mar 13, 2023 at 12:12
  • Hi @SallyCJ I'm using wp_update_attachment_metadata() to update existing images' details (principally due to the file moving location) and also wp_insert_attachment() to add new such images from remote locations - the latter, with file_get_contents() and file_put_contents(). I am writing an image organisation plugin github.com/robertandrews/wp-tidy-media Do you think I should be using the media_handle_* functions? Thanks for pointing out the default format, which is to the file. I'm aware guid actually should not be changed, but they are wrong due to image moves. Mar 13, 2023 at 21:32
  • The section "Important GUID Note" in the WordPress documentation stated that GUID 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". It also says, "Never, ever, change the contents of the GUID column, under any circumstances".
    – Sally CJ
    Mar 14, 2023 at 3:24
  • So if you or the admin on the site where your plugin is used, truly wants to change the GUID (which means they understand the consequences of doing so), then you can follow the default format (file URL). My understanding of the GUID though, is like our birth place - no matter where we are now, the fact that we were born in that place remains the same forever - i.e. we can lie, but the fact will never change...
    – Sally CJ
    Mar 14, 2023 at 3:29
  • Thanks for posting. I read that. This is for a site with changes so wide-ranging as to constitute a fresh, reborn site. Still curious what application guid has when the wp_posts type is attachment - what are feed readers doing with that? Also, should I be using the media_handle_* functions instead of file_get_contents()? Mar 14, 2023 at 17:37

1 Answer 1

1

The GUID is a unique identifier. That is its only purpose.

When it's a post, I think guid is self-evident. But what is the correct formulation/destination for that URL when it is an attachment?

Attachments are posts. If the GUID is being used then the attachment is being consumed from a feed as a post. If attachments are not being distributed in a feed then the GUID is not probably not being used either.

If it's being used as a URL then it's being misused, and the permalink should be used instead.

Should it point to the image file itself, or to the "post" page that WordPress stands up to render images?

It doesn't matter. It's only a unique identifier, it doesn't 'point' anywhere. The fact that it is a URL by default is merely the result of that being a shortcut method for creating a unique identifier, since URLs must be unique.

If it's all personal choice as long as it's unique, well, what is WordPress' default behaviour?

By default the URL to the attached file is used as the GUID, at least as far as I can tell.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.