My site is set up my site so that Users are able to submit (upload) their own
articles (posts). These posts can include linked pictures.
Then I get an email notifying me of the pending post and I review and publish it. Then the post appears on the front-end slider as a featured post.
The problem I have is that when a User submits their post and it includes a linked pictures. The image to is not automatically the “Featured Image.”
I’ve not been able to find a code that will do what I want with Linked Images. The below code I found is cut/paste into the functions.php file. I prefer a plugin, but code works too. This code works if the images are already in my “Media Library” and then inserted into the post.
=======================================================================================
function auto_set_featured() {
global $post;
$has_thumb = has_post_thumbnail($post->ID);
if (!$has_thumb) {
$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);
}
}
}
}
add_action(‘the_post’, ‘auto_set_featured’);
add_action(‘save_post’, ‘auto_set_featured’);
add_action(‘draft_to_publish’, ‘auto_set_featured’);
add_action(‘new_to_publish’, ‘auto_set_featured’);
add_action(‘pending_to_publish’, ‘auto_set_featured’);
add_action(‘future_to_publish’, ‘auto_set_featured’);
=======================================================================================