How To Auto Set Featured Image Post Thumbnails

Most themes already pull the first image attachment from within a post and display it as a featured image thumbnail on all archive pages like your home, blog, category and author archives.

Example:

featured image thumbnail archives

Some themes also display the image above all your single post titles and sometimes also above your archive titles. You can also add or remove featured images from displaying above your post titles as well depending on which theme you’re using.

If your theme doesn’t display a featured image in any location automatically, you may find this feature in your themes settings otherwise you’ll need to set a featured image on all single posts manually.

Another option is to add the function to your theme, of it isn’t already included.

Paste this code at the end of your child themes functions.php file.

All new posts will automatically use the attachment i.d for the image you have uploaded to your Media Library and entered into the code below (example ‘40370’).

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, '40370');
wp_reset_query();                                
   }
  }
 }
}
add_action('save_post', 'wpsites_auto_set_featured_image');

Simply change this line of code

set_post_thumbnail($post->ID, '40370');

to this to use a unique featured image:

set_post_thumbnail($post->ID, $attachment_id);

Basically, this code uses post image attachment number and sets it as a featured image automatically if you forget to add it yourself manually.

You need to replace the attachment i.d number with your own which you can grab after uploading a default post thumbnail to your Media Library and clicking Edit to view the i.d/ Here’s an example of what it looks like:

Edit Media

If you don’t have a picture to display, here’s some excellent web design tools for creating your own featured images.

Auto Set Default Post Image

This code will pull an image named default-image.png from your child themes images folder and display it above all single post titles and above archive page titles.

add_filter( 'post_thumbnail_html', 'wpsites_default_post_image' );

function wpsites_default_post_image( $html ) {

	if ( empty( $html ) )
		$html = '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/default-image.png' . '" alt="enter your alt text here" />';

	return $html;
}

As previously mentioned, most of the default themes like Twenty Twelve and Twenty Thirteen already have this function built into the parent themes core files meaning this code won’t work if this is the case, nor is it needed.

Default Featured Image Plugin

This plugin will add a box to your Media settings where you can set a default featured image. The featured image will display above all single posts titles and above all archive page titles.

Add default feature image

Unlike the block of code above, it will not pull an image from a single post or add the default image in your featured image module in all edit post screens.

You will also need to crop the image to the exact size you want displayed as there aren’t any settings to resize the image.

Auto Set Featured Image On Older Posts

Most of the plugins and code snippets already mentioned in this post, don’t auto set any images on older posts.

This plugin will set a featured image on all existing posts as long as at least one image has been uploaded/inserted and attached to your older posts.

Note: If you simply insert an image from the media library which has already been uploaded to another post, this plugin will not display that image on your archive pages as a featured image. The image must be uploaded and attached to each specific post for this plugin to work.

Genesis Featured Images

If you’re using the Genesis Design Framework, you’ll know that there’s a section in the theme settings under Content Archives for choosing and displaying a featured image on all archive pages. This setting will not automatically pull any images from your posts and display them unless you add the code above or manually set your featured images on all single posts manually.

genesis content archives featured images

Genesis Default Featured Image Plugin

This plugin displays a featured image on all archive pages if one is Not set manually or if you haven’t inserted an image in the content area of the single post.

genesis default featured image

Check out some of the other Genesis plugins developed specifically for this excellent premium theme.

Conclusion

Most of these solutions don’t provide much flexibility as far as resizing featured images and post thumbnails.

However, Genesis do offer built in theme settings which enable you to choose from a range of sizes to display on your archive pages unlike the default WordPress themes. You can also add more sizes to choose from simply by adding a new line in your child themes functions.php file.

Example you can use and modify:

/** Add new image sizes */
add_image_size( 'featured', 300, 150, TRUE );

More Posts About Featured Images


Comments

7 responses to “How To Auto Set Featured Image Post Thumbnails”

  1. Matt Vaden Avatar
    Matt Vaden

    Just wanted to say thanks for the helpful info and code snippet. Your code worked like a champ!

    I know this comment may seem trivial, but it never hurts to say thanks to someone. Most people have no idea the time and effort that is involved in writing detailed, informative posts on a regular basis the way you do.

    However, I do, and your contribution to this community is greatly appreciated.

    Regards,
    Matt Vaden

    1. Brad Dalton Avatar
      Brad Dalton

      You’re welcome Matt and thanks for the kind words.

  2. Hi Brad,

    how can I make this work for pages as well? I have a large number of pages (200+) that already have an image attached to them, but not set as the featured image. It’s quite a hassle to do this manually, and the way you’ve explained, it can be done automatically for posts. So then it should be possible for pages as well, am I right?

    Thanks a lot!

    1. Brad Dalton Avatar
      Brad Dalton

      Hi Bart

      Another option is to use a plugin or set a default image for pages.http://wpsites.net/web-design/php-code-to-set-genesis-default-featured-image-post-thumbnail/

  3. Thank you! I will try this.. Happy Xmas to you and your family.

  4. Brad, I’m working on a community site for my fellow artist. I want to be able to set a featured image for the default featured image. Due to the nature of some of the images being nude or not. The Facebook will pull the featured image. I would like to lock the Image I set as default for all posts.
    How could this be done?
    -Scot

    1. Brad Dalton Avatar
      Brad Dalton

      Merry Xmas Scot

      The easiest way is to install a plugin like this one http://wordpress.org/plugins/genesis-featured-images/

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.