Setting featured image for old posts automatically

I want to set featured image automatically for already published posts. I found this code snippet online but how should I edit it for already published posts?

'function wpsites_auto_set_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
wp_reset_query();
}
}
}
}
add_action('save_post', 'wpsites_auto_set_featured_image');'