WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the Meta Box plugin, everything works fine. I'm using it to create gallery function on some custom post type, and I'm able to display the uploaded images using this code:

global $wpdb;
$meta = get_post_meta( get_the_ID(), 'meta_key', false );

if ( ! is_array( $meta ) )
    $meta = ( array ) $meta;

if ( ! empty( $meta ) )
    $meta = implode( ',', $meta );
    $images = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts}
    WHERE post_type = 'attachment'
    AND ID IN ( {$meta} )
    ORDER BY menu_order ASC" );
    foreach ( $images as $att )
        // Get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
        $src = wp_get_attachment_image_src( $att, 'full' );
        $src = $src[0];
        // Show image
        echo "<img src='{$src}' />";

The images are shown, but I want to also display the captions from each picture. How can I do it?

share|improve this question
up vote 2 down vote accepted
  1. st Note:

    // Prepare the code for safety
    $images = $wpdb->get_col( $wpdb->prepare( "
        SELECT ID FROM %s
        WHERE post_type = 'attachment'
        AND ID IN ( %s )
        ORDER BY menu_order ASC
    ", $wpdb->posts, $meta ) );
  2. nd Note: Look into what you already got:

    // Inside the foreach loop
    echo '<pre>'; var_dump( $att ); echo '<pre>';
  3. rd Note: wp_get_attachment_metadata() gives you meta data. Just drop the post ID in there.

share|improve this answer
Thanks Kaiser, I figured it out how to do it! – aurrutia Apr 2 '12 at 21:09
In this case: Edit my answer to leave a complete sample for later readers :) – kaiser Apr 2 '12 at 23:59
Hello kaiser, it seems that the solution I found duplicated some images randomly :( Also, using wp_get_attachment_metadata(), the caption displayed was the one from the Exif file, not from the Media Library item. Will try to find the solution and will be posted here. Regards. – aurrutia Apr 10 '12 at 13:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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