Title: Fix for old posts
Last modified: September 1, 2016

---

# Fix for old posts

 *  [ivancasasempere](https://wordpress.org/support/users/ivancasasemepre/)
 * (@ivancasasemepre)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/fix-for-old-posts/)
 * I did some changes to the code and now it works for old posts.
 * This is the code:
 *     ```
       if ( function_exists( 'add_theme_support' ) ) {
   
       	add_theme_support( 'post-thumbnails' ); // This should be in your theme. But we add this here because this way we can have featured images before swicth to a theme that supports them.
   
       	function easy_add_thumbnail($post) {
   
       	    //$already_has_thumb = has_post_thumbnail(); //old
       	    $post_type = get_post_type( $post->ID );
       	    $exclude_types = array('');
       	    $exclude_types = apply_filters( 'eat_exclude_types', $exclude_types );
   
       		$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
   
       	    // do nothing if the post has already a featured image set
       	    if ( !empty($feat_image) ) {
       	    	return;
       	    }
       		// do the job if the post is not from an excluded type
       	    if ( ! in_array( $post_type, $exclude_types ) )  {
   
       	        // get first attached image
       	        $attached_image = get_children( "order=ASC&post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
   
       		    if ( $attached_image ) {
   
       		        $attachment_values = array_values( $attached_image );
       		        echo $attachment_values[0]->ID;
       		        // add attachment ID
       		        set_post_thumbnail( $post->ID, $attachment_values[0]->ID);
   
       		    }
   
       	    }
   
       	}
   
       	// set featured image before post is displayed (for old posts)
       	add_action('the_post', 'easy_add_thumbnail');
   
       	// hooks added to set the thumbnail when publishing too
       	add_action('new_to_publish', 'easy_add_thumbnail');
       	add_action('draft_to_publish', 'easy_add_thumbnail');
       	add_action('pending_to_publish', 'easy_add_thumbnail');
       	add_action('future_to_publish', 'easy_add_thumbnail');
   
       }
       ```
   
 * [https://wordpress.org/plugins/easy-add-thumbnail/](https://wordpress.org/plugins/easy-add-thumbnail/)

The topic ‘Fix for old posts’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/easy-add-thumbnail.svg)
 * [Easy Add Thumbnail](https://wordpress.org/plugins/easy-add-thumbnail/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/easy-add-thumbnail/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/easy-add-thumbnail/)
 * [Active Topics](https://wordpress.org/support/plugin/easy-add-thumbnail/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/easy-add-thumbnail/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/easy-add-thumbnail/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [ivancasasempere](https://wordpress.org/support/users/ivancasasemepre/)
 * Last activity: [9 years, 10 months ago](https://wordpress.org/support/topic/fix-for-old-posts/)
 * Status: not a support question