Home / WORDPRESS / Automatically Set First Image as WordPress Thumbnail Image (No Plugin Required)

Automatically Set First Image as WordPress Thumbnail Image (No Plugin Required)

If you’re interested in automating the process of setting the first image as the WordPress thumbnail image without the use of a plugin, then this guide is perfect for you.

If you’ve recently migrated your website from Blogger to WordPress or switched themes and find that no thumbnail images are set for your blog posts, this article can be a huge time-saver for you.

As you may know, the thumbnail image, also known as the featured image, is what appears on your category and tags pages. When left unset, it results in a blank space, which can look unappealing. If you have numerous blog posts and don’t want to set the featured image manually for each one, you can easily accomplish this by following the steps outlined below.

By using a simple code snippet, you can automatically set the featured image in WordPress based on the first image included in each post. This method allows you to save time and effort by setting the featured image for all your blog posts within seconds. The best part is that you can remove the code once the task is completed.

Steps to Automatically Set Featured Image (No Plugin Required)

  • Log in to your WordPress dashboard and navigate to Appearance > Theme File Editor.

  • Locate and open the functions.php file of your theme.

  • Copy and paste the provided code snippet into the functions.php file.

// 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');
  • Refresh your website, and you’ll notice that all your posts now have featured images. If some posts still lack thumbnails, you can refer to the troubleshooting methods provided below:
  1. Ensure you verify if the post includes at least one image.

  2. Additionally, ensure that the image is located in your media library and not added via an external link.

  3. Navigate to Permalink and save the settings without making any modifications.

After completing the tasks, you can return to the function.php file and delete the code.

Automatic Setting of Featured Image in WordPress using a Plugin

You can utilize plugins to automatically set your featured image on WordPress.

XO Featured Image Tools

Main Features of this Plugin

  • Establish a default thumbnail for blog posts without a thumbnail
  • Automatically create a thumbnail from the first image in the blog post
  • Prevent smaller images from being chosen as the featured image
  • Display the featured image elements in the post list
  • Utilize an external image as the featured image

Auto Featured Image (Auto Post Thumbnail)

Main Features of this Plugin

  • Instantly generate or delete featured images with a single click
  • Generate or remove featured images based on post type, status, category, or creation date
  • Automatically produce an image for each post from the post title and designate it as the featured image
  • Generate or remove featured images from a list of posts or pages (PRO)

You can also consider using this plugin:- “Get First Image Set As Featured Image.” Utilize this method only if the above two methods do not work for you.

I trust you have grasped how to bulk set a featured image in WordPress, and if you have any uncertainties, do not hesitate to ask in the comment section. I am eager to hear about your experience with this.

Leave a Reply

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