How to get an image metadata such as alt text, caption and description from media library in wordpress
Asked Answered
C

2

5

I am trying to get metadata associated with the featured image but get_post_meta keeps returning empty.

$image_alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true); 

This works and returns the data, but the code below does not work:

$image_description = get_post_meta($image_id, '_wp_attachment_description', true);
$image_caption = get_post_meta($image_id, '_wp_attachment_caption', true);

These two return empty. I filled out those fields but I can not get them back in my template!

I am trying to use Alt Text, Title, Caption and Description of the featured image to improve my website SEO, but I can not figure out why they're coming out empty.

enter image description here

I found this post and this post, but they make me even more confused.

Would you please help me?

Thank you in advance.

Condon answered 28/10, 2021 at 23:12 Comment(0)
I
11

"These two return empty"

Because in the post meta table there is no key called '_wp_attachment_description'. Same thing with '_wp_attachment_caption'. They're stored in the posts table. That's why get_post_meta returns empty!


First way

So let's say,for example, I upload one of the wordpress's logo and populate those metadata fields, like this:

enter image description hereenter image description here

Now in order to get the data you're looking for you could use attachment_url_to_postid and get_post_field functions. So you could do something like this:

$image_id = attachment_url_to_postid( 'http://yourwebsite.dev/wp-content/uploads/2021/10/WordPress-logotype-alternative-white.png' );  

$image_alt_text        = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_caption         = get_post_field('post_excerpt', $image_id);
$image_title           = get_post_field('post_title', $image_id);
$image_content         = get_post_field('post_content', $image_id);
$image_name            = get_post_field('post_name', $image_id);
$image_post_type       = get_post_field('post_type', $image_id);
$image_post_mime_type  = get_post_field('post_mime_type', $image_id);

In order to test it, we could do something like this:

echo "<strong>This is the id of the featured image:</strong><span style='color:green;'> {$image_id}</span>";
echo '<br>';
echo "<strong>This is the alternative Text:</strong><span style='color:green;'> {$image_alt_text}</span>";
echo '<br>';
echo "<strong>This is the caption:</strong><span style='color:green;'> {$image_caption}</span>";
echo '<br>';
echo "<strong>This is the title:</strong><span style='color:green;'> {$image_title}</span>";
echo '<br>';
echo "<strong>This is the description of the image:</strong><span style='color:green;'> {$image_content}</span>";
echo '<br>';
echo "<strong>This is the original name of the file:</strong><span style='color:green;'> {$image_name}</span>";
echo '<br>';
echo "<strong>This is the post type:</strong><span style='color:green;'> {$image_post_type}</span>";
echo '<br>';
echo "<strong>This is the mime type of the image:</strong><span style='color:green;'> {$image_post_mime_type}</span>";
echo '<br>

Which outputs this:

enter image description here

NOTE:

  • I used a dummy link to the image just to give you an example, so make sure to change it to the image you want to get the metadata for!
  • As you can see in the screenshot, image description is stored as post_content and image caption is stored as post_excerpt.
  • I also got the post_type and mime_type for you!

Second way, in the wordpress loop

If you want to get the metadata in the wordpress loop, we could use get_post_thumbnail_id function. So you could use the following code:

$args = array(
  'post_type' => 'post',
  'posts_per_page' => -1,
);

$query = new WP_Query($args);

if($query){
  while($query->have_posts()){
    $query->the_post();
    echo "<strong>This is the title of a post: </strong>" . get_the_title();
    $image_id = get_post_thumbnail_id(get_the_id());
    echo '<br>';
    echo "<strong>This is the id of the featured image:</strong><span style='color:green;'> {$image_id}</span>";
    echo '<br>';
    $image_alt_text        = get_post_meta($image_id, '_wp_attachment_image_alt', true);
    $image_caption         = get_post_field('post_excerpt', $image_id);
    $image_title           = get_post_field('post_title', $image_id);
    $image_content         = get_post_field('post_content', $image_id);
    $image_name            = get_post_field('post_name', $image_id);
    $image_post_type       = get_post_field('post_type', $image_id);
    $image_post_mime_type  = get_post_field('post_mime_type', $image_id);

    echo "<strong>This is the alternative Text:</strong><span style='color:green;'> {$image_alt_text}</span>";
    echo '<br>';
    echo "<strong>This is the caption:</strong><span style='color:green;'> {$image_caption}</span>";
    echo '<br>';
    echo "<strong>This is the title:</strong><span style='color:green;'> {$image_title}</span>";
    echo '<br>';
    echo "<strong>This is the description of the image:</strong><span style='color:green;'> {$image_content}</span>";
    echo '<br>';
    echo "<strong>This is the original name of the file:</strong><span style='color:green;'> {$image_name}</span>";
    echo '<br>';
    echo "<strong>This is the post type:</strong><span style='color:green;'> {$image_post_type}</span>";
    echo '<br>';
    echo "<strong>This is the mime type of the image:</strong><span style='color:green;'> {$image_post_mime_type}</span>";
  }
}

wp_reset_postdata();

Which outputs this:

enter image description here


Third way, refactoring and optimizing our code a little bit!

In both solutions above, we're repeating ourselves. So to follow "DRY" principle, we could write a reusable function which will return an associative array of the metadata related to an image id. Like so:

This function goes into the functions.php file of your theme.

/**
 * A function to retrieve corresponding metadata for an image uploaded through Media Library in Wordpress
 *
 * @author https://stackoverflow.com/users/15040627/ruvee
 * 
 * @param string|int The id of the image you're trying to get metadata for!
 * 
 * @return array This function returns an associative array of metadata related to the image id being passed to it.
 */

function getting_image_metadata($image_id){

  $image_metadata_array = array();

  $image_metadata_array['image_alt_text']        = get_post_meta($image_id, '_wp_attachment_image_alt', true) ?? '';
  $image_metadata_array['image_caption']         = get_post_field('post_excerpt', $image_id) ?? '';
  $image_metadata_array['image_title']           = get_post_field('post_title', $image_id) ?? '';
  $image_metadata_array['image_content']         = get_post_field('post_content', $image_id) ?? '';
  $image_metadata_array['image_name']            = get_post_field('post_name', $image_id) ?? '';
  $image_metadata_array['image_post_type']       = get_post_field('post_type', $image_id) ?? '';
  $image_metadata_array['image_post_mime_type']  = get_post_field('post_mime_type', $image_id) ?? '';

  return $image_metadata_array;
}

Now in order to use this function in your templates, you could do something like this:

$image_id = attachment_url_to_postid( 'http://yourwebsite.dev/wp-content/uploads/2021/10/WordPress-logotype-alternative-white.png' );

$image_metadata_array = getting_image_metadata($image_id);

echo "<pre>";
print_r($image_metadata_array);
echo "</pre>";

Or in the wordpress loop:

$image_id = get_post_thumbnail_id(get_the_id()); 

$image_metadata_array = getting_image_metadata($image_id);

echo "<pre>";
print_r($image_metadata_array);
echo "</pre>";

Which outputs this associative array:

enter image description here


Hope this answer clears things up for you!


This answer has been fully tested on wordpress 5.8 and works fine!

Inequitable answered 28/10, 2021 at 23:56 Comment(3)
Nice answer!!!!Galasyn
Wow! Worked like a charm. I honestly did not expect this detailed explanation. Thank you so much. I always find awesome help here on stackoverflow. Your answer gives me the ability to pick and choose any pieces of the metadata or remove the ones I do not need which is awesome. I like specially the reusable function. Thanks.Condon
Hi @Ruvee! I have a follow-up question. I was wondering if there is a way that I can use that function in a shortcode. Thanks.Condon
H
2

The Caption is actually the POST Excerpt and the Description is actually the POST Content

So you would do this:

/* int - the attachment id */
$image_data = get_post( int );
$description = apply_filters( 'the_content', $image_data->post_content );
$caption = apply_filters( 'the_excerpt', $image_data->post_excerpt );
Hemminger answered 28/10, 2021 at 23:45 Comment(1)
Thanks for helping me, I'll upvote your answer. Your answer works.Condon

© 2022 - 2025 — McMap. All rights reserved.