Is there a way to get attachment data?

So, I’m no big fan of how WP stores images (image data) and I was hoping there had been made some improvements in version 4.x. But alas no.

When I say image data, I mean url, title, caption, alt text, description data that you can update in the media gallery.

wp_get_attachment_image only returns a URL with this data
wp_get_attachment_image_src only returns url
get_attached_media( 'image' ) returns some data:

  public 'post_content' => string 'This is the description'
  public 'post_title' => string 'This is the title'
  public 'post_excerpt' => string 'This is the caption'
  public 'guid' => string 'http://somesite.com/wp-content/uploads/2015/10/test.jpg' 

After I removed the attachment (and deleted the image), then attached another image, get_attached_media( 'image' ) returns empty.

So IS there any good way of getting image data?

1 Answer
1

Don’t forget that attachments are just posts (though they are native posts and those tend to be a special case).

Thus simply get_post() on attachment ID should get you typical post object for it. 🙂 Similarly all API functions that work on posts should work on attachments.

Leave a Comment