Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created March 22, 2016 14: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 kellenmace/7ad55c52f6e85591ca1b to your computer and use it in GitHub Desktop.
Save kellenmace/7ad55c52f6e85591ca1b to your computer and use it in GitHub Desktop.
<?php
// 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