Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// The IDs of the posts for which you want to update the featured image caption
$post_ids_to_update = array( 21, 57, 95 );
// Loop through each of the post IDs
foreach ( $post_ids_to_update as $post_id ) {
// Get the ID of this post's featured image
$featured_image_id = get_post_thumbnail_id( $post_id );
// Get the title of this post's featured image
$featured_image_title = get_the_title( $featured_image_id );
// Sanitize the text so we're ready to insert it into the database
$new_caption_text = sanitize_text_field( $featured_image_title );
// Set the new caption (a.k.a. the post excerpt) of the featured image
wp_update_post( 'post_excerpt', $featured_image_title )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment