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?


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.

| improve this answer | |
  • 1
    I know, that's why I don't like how images are stored. But now that you mention it, creating a function that uses get_post and return the image data is a the way to go :) – Steven Oct 28 '15 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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