Title: Adding attachment to post using wp_insert_post
Last modified: August 20, 2016

---

# Adding attachment to post using wp_insert_post

 *  [wheremyfilesat.com](https://wordpress.org/support/users/wheremyfilesatcom/)
 * (@wheremyfilesatcom)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/adding-attachment-to-post-using-wp_insert_post/)
 * Hi all,
    couple weeks ago i got my first wordpress site up and running, now im
   trying to make a custom home page for it.
 * I have a bash script on my server that produces a php page to call wordpresses
   wp_insert_post function, this works fine.
 * However, im using a plugin on my home page to list post from specific categories,
   this plugin inserts a thumbnail next to the the post.
 * but im having trouble adding a thumbnail to my post, using the script. if i create
   a post manually and choose the ‘set featured image’ button on the post creation
   page my thumbnail shows up.
 * Heres what i have so far, this will insert the image into the wordpress media
   gallery, but i cant get it to add to the post.
 *     ```
       <?php
         require_once(ABSPATH . 'wp-includes/functions.php');
         require_once(ABSPATH . 'wp-admin/includes/image.php');
         include_once"wp-config.php";
         include_once"wp-load.php";
         include_once"wp-includes/wp-db.php";
   
         $filename = "http://188.72.223.152/wheremyfilesat/wp-content/uploads/xelb1cnn.jpg";
         $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, 866 );
   
         $thumb_attach_attr = wp_get_attachment_image_src( $attach_id );
   
         $postmeta_id = add_post_meta(866, 'thumbnail', $thumb_attach_attr[0]);
   
         // you must first include the image.php file
         // for the function wp_generate_attachment_metadata() to work
         $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
         wp_update_attachment_metadata( $attach_id, $attach_data );
       ?>
       ```
   
 * 866 is the id of the post i want the thumbnail inserted to
 * Ive almost have my page setup the way i want, its this little thing that is driving
   me crazy
    thanks

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

 *  [refulez](https://wordpress.org/support/users/refulez/)
 * (@refulez)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/adding-attachment-to-post-using-wp_insert_post/#post-2229255)
 * [http://wordpress.org/support/topic/get-and-resize-image-thumbnails-with-images-taken-from-custom-fields?replies=7](http://wordpress.org/support/topic/get-and-resize-image-thumbnails-with-images-taken-from-custom-fields?replies=7)
 *  Thread Starter [wheremyfilesat.com](https://wordpress.org/support/users/wheremyfilesatcom/)
 * (@wheremyfilesatcom)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/adding-attachment-to-post-using-wp_insert_post/#post-2229264)
 * omg thnx so much, that had been driving me crazy for sooo long
 * i was missing one simple line
    `set_post_thumbnail( 866, $attach_id );`
 * i must have missed that on the codex somehow
 * here my final code incase it helps anyone
 *     ```
       $post_id = 866; // this value is retrived with '$post_id = wp_insert_post(....)'
         $filename = "http://www.wheremyfilesat.com/wp-content/uploads/xelb1cnn.jpg";
         $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 );
   
         $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
         wp_update_attachment_metadata( $attach_id, $attach_data );
   
         set_post_thumbnail( $post_id, $attach_id );
       ```
   
 * Thanks again

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

The topic ‘Adding attachment to post using wp_insert_post’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [wheremyfilesat.com](https://wordpress.org/support/users/wheremyfilesatcom/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/adding-attachment-to-post-using-wp_insert_post/#post-2229264)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
