I have a blog with a lot (over 1000) images. Each image has a meaningful descriptive filename. Wordpress has title caption and description fields for images. What's the easiest way to batch process all images and copy the file name to the title alt description and caption fields. Also if I could update yoast seo fields too that would be great.


You can make a query to loop through all the attachments, and then update the attachment info for each attachment in the while loop.

Something like

$args = array(
   'post_type' => 'attachments',
   'post_status' => 'any',
   'posts_per_page' => -1,

$query = new WP_Query($args)

while($query->have_posts()): $query->the_post();

// 1. Get the attachment filename here and store it in a variable eg. $filename. See comment at the end of this answer

// 2. Update the post title
$attachment_post = array(
   'ID' => get_the_id();
   'post_title' => $filename

endwhile; wp_reset_postdata(); endif;

Did a quick search on how to get the attachment filename but couldn't find it but this should get you started. Within the loop you should also be able to update the description. You can look at wp_update_attachment_metadata and als check this post (related to the filename).

  • Thanks eskimo, I ended up just writing a php file to quickly do it for me. Your solution works I'm sure, but would have involved making a plugin. Next I need to find a way to set the other attributes to filename on upload.. – S.. Nov 15 '13 at 12:55
  • 1
    NP. It wouldn't involve making a plugin though, you can paste the code from my answer about anywhere (in a page template for example). I'm sure there's a hook for when files get uploaded that you can use. – eskimo Nov 18 '13 at 16:58

I ended up writing a php file to quickly solve the problem.

$s_query = "SELECT ID, post_title FROM `wp_posts` WHERE NOT post_content <=> post_title AND post_type='attachment'";
    $r_result = mysql_query($s_query);

        while($row = mysql_fetch_row($r_result)){
            $s_update_query = 'UPDATE `wp_posts` SET post_content="'.$row[1].'", post_excerpt="'.$row[1].'" WHERE ID="'.$row[0].'"';
  • That's a nice solution! – eskimo Nov 15 '13 at 13:32

Well, I was using lightbox gallery, and I needed to have the post title for my images as the alt text.

What I did was create a query on the database to update the post_excerpt to match the post title.

UPDATE wp_posts SET `post_excerpt` = `post_title` WHERE post_type = 'attachment'

This seem to work for me and the lightbox gallery.

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.