Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this piece of code that I am calling within the loop to get image metadata ( title/caption and description ). It works fine but it only works for the first post in the loop. So I assume it needs to be reset. From looking here I think wp_rest_postdata(); is the right way to reset the query, but still the output fails for all but the first post, it repeats the data from the first post for every other post.

function get_image_metadata() {

    $image_id = get_post_thumbnail_id($post->ID);
    $args = array(
        'post_type' => 'attachment',
        'post_status' => null,
        'post_parent' => $post->ID,
        'include'  => $image_id
        ); 
    $image_data = get_posts($args);

    if ($image_data && isset($image_data[0])) {
        $meta_output['title'] = $image_data[0]->post_title; 
        $meta_output['caption'] =  $image_data[0]->post_excerpt; 
        $meta_output['description'] =  $image_data[0]->post_content; 
    }
    //wp_reset_query();
    wp_rest_postdata();
    return $meta_output;
}
share|improve this question
    
Can you post the template-file/loop context in which this function is called, for reference? –  Chip Bennett Aug 2 '12 at 2:14

1 Answer 1

up vote 2 down vote accepted

When using get_posts() you don't actually setup post data unless you do so explicitly, via setup_postdata() inside your (usually a foreach) loop.

So, since you're not setting up post data, you shouldn't need to reset post data.

but still the output fails for all but the first post

I think that's because you're calling the global $post inside your function, but aren't globalizing it first:

$image_id = get_post_thumbnail_id($post->ID);
$args = array(
    'post_type' => 'attachment',
    'post_status' => null,
    'post_parent' => $post->ID,
    'include'  => $image_id
    ); 
$image_data = get_posts($args);

Try adding global $post; before using $post->ID:

global $post;
$image_id = get_post_thumbnail_id($post->ID);
$args = array(
    'post_type' => 'attachment',
    'post_status' => null,
    'post_parent' => $post->ID,
    'include'  => $image_id
    ); 
$image_data = get_posts($args);

If that's not the problem, can you clarify:

  1. What data you get from $iamge_data
  2. The context in which you are calling the function
share|improve this answer
    
I had tried that but it didnt work either. The difference between globalizing $post and not is that when I don't I get the first post's output repeated for every other post and when I do I get a null output for those other posts. Actually I'm going to edit my question a bit because I think I had that wrong in there. The data I am getting is the caption, description and title of the attached image. The context I use it in is that I am calling it from within the look in content-image.php ( image post format ) –  byronyasgur Aug 2 '12 at 2:01
    
Actually my answer to your question got me thinking and I realised what the problem is. The code was actually fine, it displays the information for the featured image, not the attached image - I forgot I was using get_post_thumbnail_id , and in the other posts there were no featured images. (Yet at the same time I was displaying either featured or attached ; whichever the code found first ) ... when I added featured images to the posts it all worked fine, or at least I know what the problem was. –  byronyasgur Aug 2 '12 at 2:11
    
Score! Glad you figured it out. :) –  Chip Bennett Aug 2 '12 at 2:14

Your Answer

 
discard

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.