Get meta data from image

How can I get the description of the image?

$album_id = get_the_id();
$photos = $wpdb->get_results(
    "select * from wp_postmeta where post_id = '"
    . $album_id
    . "' AND meta_key = 'gallery_ph' order by meta_id desc"
);

This is the result of the SQL query:

Array (  
    [0] => stdClass Object (
        [meta_id] => 887
        [post_id] => 604
        [meta_key] => gallery_ph
        [meta_value] => http://xxx/wp-content/uploads/2013/03/foto_03_copy200.jpg
    )
)

Related posts

Leave a Reply

1 comment

  1. Image data is stored as if it were a post, or a CPT, so you can treat it like one.

    $album_id = get_the_id();
    $img = new WP_Query(array('p'=>$album_id,'post_type'=>'attachment'));
    var_dump($img->posts[0]->post_content);
    

    Or, a little more complicated,…

    $album_id = get_the_id();
    $img = new WP_Query(array('p'=>$album_id,'post_type'=>'attachment'));
    if (!empty($img->posts[0])) {
        var_dump($img->posts[0]->post_content);
    }
    

    get_the_ID will return the ID of the current post so that will only work on an attachment page. I assume, since that is what you used, that is the context in which this is meant to work.

    Reference

    https://codex.wordpress.org/Class_Reference/WP_Query