Automatically Set Featured Image in WordPress

  • follow us in feedly
Published May 4, 2013 by Brad Knutson
Auto Set Featured Image in WordPress

Featured images in WordPress are a good way to tie an image to a post. This image is often times meant to be an overview, or advertising for the post. The image can be shown in RSS feeds, Twitter cards, Google+ shares, Facebook shares, and more. They can be the gateway into the post.

Many bloggers put a lot of effort into creating these featured posts. Often times the look and feel of their blog depends on having a featured image. Take my blog for example. I use featured images on every single post. I used one on this post.

But what if we just want to blog, and not worry about creating featured images? Is there a way to automatically set the first image in the post as the featured image? This could be a huge time saver!

Well, thankfully there is. I’ve seen several such functions across multiple blogs, and decided to clean some of them up. The function below should accomplish exactly what we’ve talked about.

Add the below code to your themes functions.php file.

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');

Essentially, we’re grabbing the first image in the post, and setting it as the featured image using the built-in set_post_thumbnail() WordPress function. We are hooking into several actions, and setting the image every time we save, publish, or update a post. The featured image will always be the first image in the post, so if you update the image or post and add a new image at the top, it will be set as the featured image.

There you have it – short post today. Working on some bigger posts in the near future!

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date

Subscribe

Topics

See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Video SEO by Yoast SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

16 thoughts on “Automatically Set Featured Image in WordPress

    1. Brad Knutson Post author

      Have you tried publishing a post since you added this to your themes functions.php file? The code works when you publish a post (or save one).

      Hope this helps!

      Thanks,
      Brad

    1. Jessica brant

      I do have a question for you. For the life of me I cannot figure out how to remove the set feature image from the top of the blog post. I need it to be set to feature image so the first image appears on my main page and in recent post but I can’t stand that it is set to the top of the page and with in the post.

      Is there any way to remove that image grime appearing twice? At the top of the page and first image in post??

      Help please.
      Jess

    2. Brad Knutson Post author

      If I’m understanding you correctly, you want the featured image to display on your home page, and the recent posts page (which is currently does), but you do not want the featured image to display on the actual post page (which is also currently does).

      If you don’t want the featured image to display on certain page templates, you’ll have to modify the PHP and remove the line of code that echos the_post_thumbnail();. For example, on this site, I use a file named single-blog.php to display my blog posts – if I didn’t want the featured image to be displayed at the top I would remove the line of code from this template only – this way it will still display on the home page and main blog page.

      Does that make sense? Hope it helps!

  1. damian

    Hello Brad,

    Anyway to retroactivate this or create a plugin that runs a script that would do it. There was a plugin I tried, but it refused to install.

    Reply
    1. damian

      Actually I think it did work. The widget I’m using – ‘Featured Category Posts Widget’ doesn’t seem to offer WordPress default image options, it only shows slider-image,shop-thumbnail,slider-thumb,carousel-thumb,shop-single,shop-catalog. So it’s displaying when I select shop-thumbnail, but I don’t know if that’s because of or in spite of (eg, possibly Woocommerce is automatically generating thumbnails from the first image?). Also, when going to Post Edit, seeing in the bottom right hand corner a link to the image (but not an actual image, I assume this is correct behavior) under ‘Featured Image’, that is definitely new. So in short, it seems to be working.

    1. Brad Knutson Post author

      This method updates the featured image on Save, Update or Publish – so unfortunately if you want to use this on existing posts you’ll have to go back, open them up, and hit Update.

      I’ll look for a better solution but that’s all I’m aware of at this time.

    2. Aaron

      Hey, thanks for your quick reply!
      I actually tried updating the posts but it’s still not working. I imported the posts from a rss feed, maybe that’s the reason?
      Also, are you familiar with this plugin http://wordpress.org/plugins/media-tools/? It seems to be working for most people but it would cause me a database error every time. Thought I’d recommend it here for others.

  2. Levon

    This looks great.

    I have a question however. I have linked images to my post because they are hosted elsewhere. Will this code still work in my circumstance?

    Reply
    1. Brad Knutson Post author

      I believe you’ll need to host the images through your WordPress installation (so it has an attachment_id) for this to work.

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">