1

If I do a query of the post table for post type 'attachment' sometimes the guid field will contain an attachment id, "http://sitename.com/?attachment_id=1201", instead of a URL. How can I convert a string like this into the attachment url?

2 Answers 2

1

GUIDs aren't URLs, they are unique identifiers for feed readers. The GUID should never be used in any other way.

See wp_get_attachment_url and get_attachment_link.

1

I found that part of the actual path, relative to the uploads folder, is stored in the postmeta table for the meta_key of '_wp_attached_file' which can be obtained, as Milo suggested, by using wp_get_attachment_url.

2
  • This is kind of just a coincidence. So NEVER use this to get a URL, there are countless situations this will fail badly.
    – kraftner
    Oct 22, 2015 at 14:12
  • Agreed. From now on I will use the path from the postmeta table.
    – AlanP
    Oct 27, 2015 at 12:14

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.