How to Automatically set First Image as Wordpress Thumbnail Image (Without Plugin)

Auto Set Featured image

If you are looking to Auto Generate Featured Image in WordPress Posts then this Article is for you.

If you have recently Migrated your Website from Blogger to Wordpress or changed the theme and there is no thumbnail image is set in all of your Blog post then this Article can save you a lot of time.

As you know A thumbnail image or Featured image is the image that shows in your category and tags page and if you don’t set it then it will show blank and looks odd. And when you have a lot of blog posts and you don’t want to set featured image manually one by one then you can follow the steps as shown in this video.

Here, We can auto set Featured image in Wordpress just by adding a small code snippet and all of your Blog post will automatically get the featured image based on the first image added in them.

In this way, You can automatically set Featured image with in few seconds and save hours of time and effort. And the Best part is that You can remove the code once the work is done.

Steps to Auto Set Featured image (Without Plugin)

YouTube video

First of all, You need to Login to your Wordpress dashboard and Go to the Appearance and Theme file editor. Now you need to Open the Function.php file of your theme and paste the below code on it.

// Auto Set featured image in WordPress
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);}
         }
      }
}
add_action('the_post', 'wpsites_auto_set_featured_image');

Now Go to your Website and Refresh it and you can see that all of your Posts now have featured image. If you still found that some of your post doesn’t have thumbnail set then you can follow these troubleshooting methods.

  1. Make sure to check if the post has at least one image added.
  2. Also, Make sure the image is on your media Library and doesn’t added through external Link
  3. Go to Permalink and save the settings without making any changes.

Once everything is done, You can go back to the function.php file and remove the code.

If this Code snippet is not working then you can ask in the comment section.

Auto Set Featured image in WordPress using Plugin

You can also use some plugins to automatically set your featured image on WordPress.

XO Featured Image Tools

XO Featured Image Tools

Key Features of this Plugin

  • Set a default thumbnail for Blog post that doesn’t have thumbnail
  • Auto Generate thumbnail From the First image of the Blog post
  • Exclude smaller image from being set a featured image
  • Display the featured image items in the Post list.
  • Use External Image as Featured image.

Auto Featured Image (Auto Post Thumbnail)

Auto Featured Image (Auto Post Thumbnail)

Key Features of this Plugin

  • Generate or remove featured images in one click.
  • Generate or remove featured images based on the post type, status, category, or the creation date
  • Automatically generates an image for each post from the post title and sets it as the featured image
  • Generate or remove featured images from the list of posts or pages (PRO)

You can also use this plugin — “Get First Image Set As Featured Image“. Although Use this only if the above two method doesn’t work for you.

I hope you have learned How to Bulk Set Featured image in Wordpress and if you still have doubt, feel free to ask in the comment section. I love to hear your experience on this.

Related Articles..

Leave a Reply

Your email address will not be published. Required fields are marked *

2 Comments

  1. Hi!
    Your code is the only thing that worked for me, the plugin didn’t. However, for some reason is picking the SECONG image, not the first one. Any ideas on how to fix it?