Support » Fixing WordPress » automatically set the featured image

  • jojimori


    Is there a way that the Featured Image of a post can be set automatically without having to select it within the “add post” page?

    so for example in either of the following 2 cases you should be able to see what i mean.
    1. when I create a new post, I insert one image in the post, and then some text, then I publish this. Note, that I haven’t set the featured image, but I would like the featured image to default to the single image that has been included in the post.
    2. the second case is that if I wanted to use “post over email”, I can send an image for the post no problem, but I would like that image to also be the “Featured Image’ of that post, but I don’t see any way that this can be done.

    Be great if anybody had any advice on this one!

    Thanks in advance

Viewing 7 replies - 1 through 7 (of 7 total)
  • I already solved this issue for more information check my article. [Link moderated.]

    Hi jojimori,

    This really a late reply but since I’m also had encountered such problem(automatically add images as featured images of the post). For a several days I try to get the solution and do some research in internet. Lastly I found the below solution(where the code is from this codex documentation page. I just make it as a function and added the last line for making it as a featured images.

    Hopefully this will help other, remember the $post_id is the parent post ID(where the image been attach) and the $filename is the absolute path of the image files

    function set_featured_image($post_id,$filename) {
    	$wp_filetype = wp_check_filetype(basename($filename), null );
    	$attachment = array(
    		'post_mime_type' => $wp_filetype['type'],
    		'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
    		'post_content' => '',
    		'post_status' => 'inherit'
    	$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
    	// you must first include the image.php file
    	// for the function wp_generate_attachment_metadata() to work
    	require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    	$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    	if (wp_update_attachment_metadata( $attach_id,  $attach_data )) {
    		// set as featured image
    		return update_post_meta($post_id, '_thumbnail_id', $attach_id);

    @ ZONIRC

    Thanks! Any clue as to where to implement this code?


    You can put it into your theme functions.php.

    Possible it needs to go into funcitons.php?

    I tried in my theme functions.php and it didn’t work.


    Sorry for a late reply, yes it should be ok in functions.php of the theme.

    Another thing, the theme should be enable/support the featured image/post_thumbnail. To do this, you can use something like this: add_theme_support('post-thumbnails');

    To automatically add the 1st image in the post as a featured images, you need to customised it a little bit. Since the code just for assign an image as a featured image/post thumbnail for the post. Probably by hook into post save.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘automatically set the featured image’ is closed to new replies.