Skip to content

Instantly share code, notes, and snippets.

@markusvonplunkett
Created October 15, 2018 11:40
Show Gist options
  • Save markusvonplunkett/1ab016ee2a07150bc6dea28958ee3267 to your computer and use it in GitHub Desktop.
Save markusvonplunkett/1ab016ee2a07150bc6dea28958ee3267 to your computer and use it in GitHub Desktop.
Rename media titles based on the woocommerce product name
function rename_featured_images_to_post_title($post_type){
$posts = get_posts(['post_type' => $post_type, 'posts_per_page' => -1]);
foreach($posts as $post){
$title = $post->post_title;
$name = $post->post_name;
$thumbnail = get_post_thumbnail_id( $post );
if((bool)$thumbnail){
if(get_the_title($thumbnail) != $title){
wp_update_post([
'ID' => $thumbnail,
'post_title' => $title
]);
}
}
}
die();
}
if(isset($_GET['rename_featured_images'])){
rename_featured_images_to_post_title('product');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment