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;
}