Skip to content

Instantly share code, notes, and snippets.

@omniacode
Created January 30, 2025 19:19
Show Gist options
  • Select an option

  • Save omniacode/93e0a2f622157d2794688729eb881239 to your computer and use it in GitHub Desktop.

Select an option

Save omniacode/93e0a2f622157d2794688729eb881239 to your computer and use it in GitHub Desktop.
Copy Wordpress Media Title Attribute to Alt and Caption
-- Copy Title to Alt Text (_wp_attachment_image_alt), only if Alt text is missing
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT p.ID, '_wp_attachment_image_alt', p.post_title
FROM wp_posts p
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '_wp_attachment_image_alt'
WHERE p.post_type = 'attachment'
AND p.post_mime_type LIKE 'image/%'
AND pm.meta_id IS NULL;
-- Copy Title to Caption (post_excerpt), only if caption is missing
UPDATE wp_posts
SET post_excerpt = post_title
WHERE post_type = 'attachment'
AND post_mime_type LIKE 'image/%'
AND (post_excerpt IS NULL OR post_excerpt = '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment