Skip to content

Instantly share code, notes, and snippets.

@benjibee
Created December 16, 2020 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjibee/490116477a44ebe5154cd3ff147b7027 to your computer and use it in GitHub Desktop.
Save benjibee/490116477a44ebe5154cd3ff147b7027 to your computer and use it in GitHub Desktop.
Update WordPress attachment meta
$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