19 05 2011
Automatically add alt tags to wordpress featured images
I was working on a site recently that had a few thousand images imported to wordpress after a drupal conversion, these images were set as the featured image for the imported posts. Because of the way the old site was constructed, these images had no meaningful names.
You can use the mysql query below to set the alt tags of your featured images to the name of the post.
This query assumes that no alt tag data currently exists, if you have existing alt tags, you will need to use an update query instead. Also note this only sets the alt data for images set as featured images for posts.
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT pm.meta_value image_post_id, '_wp_attachment_image_alt', p.post_title
FROM wp_postmeta pm
LEFT JOIN wp_posts p ON p.id = pm.post_id
WHERE pm.meta_key = '_thumbnail_id'
AND pm.meta_value IS NOT NULL