Created
December 16, 2020 17:02
-
-
Save benjibee/490116477a44ebe5154cd3ff147b7027 to your computer and use it in GitHub Desktop.
Update WordPress attachment meta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$args = [ | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'numberposts' => -1, | |
'post_status' => null, | |
'post_parent' => null, | |
]; | |
$attachments = get_posts($args); | |
foreach ($attachments as $post) { | |
// empty anything in content (when we update it at the end!) | |
$post_update = [ | |
'ID' => $post->ID, | |
'post_content' => '', | |
'post_title' => '' | |
]; | |
// if we have content, move it to the caption | |
if ($post->post_content) { | |
$post_update['post_excerpt'] = $post->post_content; | |
} | |
// if we have any exerpt, copy it to the alt text field | |
if ($post->post_excerpt) { | |
// update the post's meta-data | |
update_post_meta($post->ID, '_wp_attachment_image_alt', $post->post_excerpt); | |
} | |
// update the post | |
$update = wp_update_post($post_update, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment