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:
wp_handle_upload()
and then manually callwp_insert_post()
? Because core functions likemedia_handle_upload()
andmedia_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 likehttps://example.com/wp-content/uploads/2023/02/image.jpg
.wp_update_attachment_metadata()
to update existing images' details (principally due to the file moving location) and alsowp_insert_attachment()
to add new such images from remote locations - the latter, withfile_get_contents()
andfile_put_contents()
. I am writing an image organisation plugin github.com/robertandrews/wp-tidy-media Do you think I should be using themedia_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.guid
has when thewp_posts
type isattachment
- what are feed readers doing with that? Also, should I be using themedia_handle_*
functions instead offile_get_contents()
?