Title: programmatically create posts with a post thumbnail?
Last modified: August 20, 2016

---

# programmatically create posts with a post thumbnail?

 *  [jfigaro](https://wordpress.org/support/users/jfigaro/)
 * (@jfigaro)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/)
 * hey, guys. still attempting to assign thumbnails to posts, if anyone could help
   it would be appreciated.
 * I’m currently retrieving listings from a feed and creating posts for each. some
   listings contain images; these listings will need one of the images to be set
   as the post thumbnail.
 * do I need to save the images to the server prior to using them for thumbnails?
   currently, the ‘src’ attribute of every image is being saved into the database.
 * thank you in advance.

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [digibucc](https://wordpress.org/support/users/digibucc/)
 * (@digibucc)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269005)
 * here is the code i use on a custom program to add an image to a post, you need
   the post id. you can get that when you run “wp_insert_attachment” , it returns
   the id.
 *     ```
       $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, $parent_id );
       $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
       wp_update_attachment_metadata( $attach_id, $attach_data );
       ```
   
 * if it’s the first/only image attachment, it will get used as the thumbnail. if
   not you can assign it as one with:
    `add_post_meta($post_id, '_thumbnail_id',
   $attach_id, true);`
 * though, you need to make sure it is the right size. if it’s too big it will only
   show part of it as the thumbnail. for that, i implemented a php image resize 
   function, which is common, google.
 *  Thread Starter [jfigaro](https://wordpress.org/support/users/jfigaro/)
 * (@jfigaro)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269133)
 * thanks digi. do I need to save images from a listing to my server, or can wp 
   insert attachments via url?
 *  [digibucc](https://wordpress.org/support/users/digibucc/)
 * (@digibucc)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269140)
 * url should be no problem, as $filename is just a path to the image. make it absolute
   with http:// and there should be no problem.
 * idk for sure, but i would be really surprised if it didn’t simply work, if that’s
   the case though – comment back. any number of possible fixes are coming to mind
   😉
 *  [Yoga Sukma](https://wordpress.org/support/users/yogasukma/)
 * (@yogasukma)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269142)
 * why not use a “featured image” ?
 *  [Rev. Voodoo](https://wordpress.org/support/users/rvoodoo/)
 * (@rvoodoo)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269143)
 * [@yogasukma](https://wordpress.org/support/users/yogasukma/): thumbnail IS featured
   image. They are one and the same. And he is trying to set the featured image 
   through php
 *  [harvitronix](https://wordpress.org/support/users/harvitronix/)
 * (@harvitronix)
 * [14 years ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269262)
 * I’m using the code digibucc suggested, and it’s nearly working.
 * The image is being inserted into the media section, and it is being attached 
   to the post. But it won’t show up as the thumbnail/featured image.
 * Here is my code:
 *     ```
       $cur_post_id = wp_insert_post( $my_post );
   
       $filename = "/wp/wp-content/uploads/2012/03/thumb13.png";
       $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, $cur_post_id );
       $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
       wp_update_attachment_metadata( $attach_id, $attach_data );
       add_post_meta($cur_post_id, '_thumbnail_id', $attach_id, true);
       ```
   
 * Any idea why WordPress isn’t recognizing it as the thumb?
 * Thanks a lot for the help!
    Matt.
 *  [harvitronix](https://wordpress.org/support/users/harvitronix/)
 * (@harvitronix)
 * [14 years ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269263)
 * Figured it out. For future explorers of this thread:
 *     ```
       $cur_post_id = wp_insert_post( $my_post );
   
       $filename = "/path/to/local/file.png";
   
       $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, $cur_post_id );
       add_post_meta($cur_post_id, '_thumbnail_id', $attach_id, true);
       ```
   
 * Cheers,
    Matt.
 *  [zschuessler](https://wordpress.org/support/users/zschuessler/)
 * (@zschuessler)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269269)
 * [@harvitronix](https://wordpress.org/support/users/harvitronix/) – Not bad, but
   you aren’t getting resized thumbnails with your code.
 * Example on bottom of this page with correct code to use:
    [http://codex.wordpress.org/Function_Reference/wp_insert_attachment](http://codex.wordpress.org/Function_Reference/wp_insert_attachment)

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘programmatically create posts with a post thumbnail?’ is closed to new
replies.

## Tags

 * [images](https://wordpress.org/support/topic-tag/images/)
 * [thumbnails](https://wordpress.org/support/topic-tag/thumbnails/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 8 replies
 * 6 participants
 * Last reply from: [zschuessler](https://wordpress.org/support/users/zschuessler/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/programmatically-create-posts-with-a-post-thumbnail/#post-2269269)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
