Featured images are very important part of modern WordPress themes and modern WordPress sites. Post Thumbnails was initially introduce in WordPress Version Version 2.9 which was changed to Featured Images with Version 3.0.
Sometimes you publish the post & then you realize that the featured post is not set for the article, so again you need to go back and update the post.
What is the solution?
The best solution is to do something which will automatically set the featured image in WordPress sites.
How to automatically set the featured image in WordPress?
We have a few ways to automatically set the featured image in WordPress based sites.
1. Auto Post Thumbnail
Auto Post Thumbnail is a plugin which will automatically set the featured image in WordPress by generating post thumbnail from the first image in post or any custom post type only if Post Thumbnail is not set.
If the post thumbnail is already present, the plugin will do nothing. If you don’t want a post thumbnail for some post with images, just add a custom field skip_post_thumb to the post and the plugin will restrain itself from generating post thumbnail.
2. Modify Function.php
Another way to automatically set the featured image in WordPress sites is by modifying the function.php file of your theme.
Add this snippet to your theme’s functions.php file.
<?php function autoset_featured() { global $post; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_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', 'autoset_featured'); add_action('save_post', 'autoset_featured'); add_action('draft_to_publish', 'autoset_featured'); add_action('new_to_publish', 'autoset_featured'); add_action('pending_to_publish', 'autoset_featured'); add_action('future_to_publish', 'autoset_featured'); ?>
Code courtesy wp-snippet.com
Don’t forget to backup our database before changing the function.php file of your theme.
Check few other useful tutorials, plugins & themes on indexwp. Keep your self updated with all the news from WordPress community by subscribing to indexwp
Thanks Dhiraj! This helped me out 🙂
thanks a bunch Dhiraj, it simply made me using less plugin 🙂
You are welcome Daniel