1

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
    )
)
  • The description is stored in the post_content field in wp_posts, rather than wp_postmeta. – Andrew Bartel Apr 19 '13 at 20:27
2

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

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.