---
description: >-
  https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment
---

# Programmatically adding Wordpress post with attachment

84

I am getting post\_title, post\_content and other things in $\_REQUEST as well as an image file. I want to save all that as a post in the wordpress database. I have on my page

```
<?php
require_once("wp-config.php");
$user_ID; //getting it from my function
$post_title = $_REQUEST['post_title'];
$post_content = $_REQUEST['post_content'];
$post_cat_id = $_REQUEST['post_cat_id']; //category ID of the post
$filename = $_FILES['image']['name'];

//I got this all in a array

$postarr = array(
 'post_status' => 'publish',
 'post_type' => 'post',
 'post_title' => $post_title,
 'post_content' => $post_content,
 'post_author' => $user_ID,
 'post_category' => array($category) 
 );
$post_id = wp_insert_post($postarr);

?>
```

This will get all the things in database as post but I don't know how to add the attachment and its post meta.

How can I do that? Can anybody help me? I am really confused and have spent a few days trying to solve this.[php](https://stackoverflow.com/questions/tagged/php)[wordpress](https://stackoverflow.com/questions/tagged/wordpress)[Share](https://stackoverflow.com/q/3805603)[Improve this question](https://stackoverflow.com/posts/3805603/edit)Follow[edited Sep 10 '17 at 5:19](https://stackoverflow.com/posts/3805603/revisions)[![](https://www.gravatar.com/avatar/e8891617e36b8d1254eb3ce1a5662da4?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/1033581/c%C5%93ur)[Cœur](https://stackoverflow.com/users/1033581/c%C5%93ur)32.3k2121 gold badges173173 silver badges231231 bronze badgesasked Sep 27 '10 at 16:00[![](https://www.gravatar.com/avatar/51e6c7236b822a61dc153c26751ff06f?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/459690/salman-khimani)[Salman Khimani](https://stackoverflow.com/users/459690/salman-khimani)51522 gold badges77 silver badges2121 bronze badges

* You should be including the wp-load.php instead of config file. – [John Magnolia](https://stackoverflow.com/users/560287/john-magnolia) [May 21 '13 at 6:58](https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment#comment23974073_3805603)

[Add a comment](https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment)

### 2 Answers

[Active](https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment?answertab=active#tab-top)[Oldest](https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment?answertab=oldest#tab-top)[Votes](https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment?answertab=votes#tab-top)8

To add an attachment, use wp\_insert\_attachment():

[https://developer.wordpress.org/reference/functions/wp\_insert\_attachment/](https://developer.wordpress.org/reference/functions/wp_insert_attachment/)

EXAMPLE:

```
<?php
  $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, 37 );
  // 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 );
  wp_update_attachment_metadata( $attach_id,  $attach_data );
?>
```

To add Meta Data, use wp\_update\_attachment\_metadata():

[https://developer.wordpress.org/reference/functions/wp\_update\_attachment\_metadata/](https://developer.wordpress.org/reference/functions/wp_update_attachment_metadata/)[Share](https://stackoverflow.com/a/3806007)[Improve this answer](https://stackoverflow.com/posts/3806007/edit)Follow[edited Jul 5 '20 at 16:46](https://stackoverflow.com/posts/3806007/revisions)[![](https://www.gravatar.com/avatar/7c2b57b3e1c503813e0020309525409a?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/1298086/matty)[matty](https://stackoverflow.com/users/1298086/matty)1,2891212 silver badges2121 bronze badgesanswered Sep 27 '10 at 16:55[![](https://www.gravatar.com/avatar/a06e0137aec91f26b0bc1d736c574bad?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/154513/todd-moses)[Todd Moses](https://stackoverflow.com/users/154513/todd-moses)10.8k1010 gold badges4444 silver badges6464 bronze badges

* 1i think it it just a copy paste from that url..... can u tell me how can i use my variables in it? Will it upload image coming through request to the wp-content/uploads ?? – [Salman Khimani](https://stackoverflow.com/users/459690/salman-khimani) [Sep 28 '10 at 6:00](https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment#comment4041360_3806007)
* $post\_content goes to post\_content, $post\_id you get from the post insert, etc... – [Todd Moses](https://stackoverflow.com/users/154513/todd-moses) [Sep 28 '10 at 17:22](https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment#comment4047771_3806007)

[Add a comment](https://stackoverflow.com/questions/3805603/programmatically-adding-wordpress-post-with-attachment)Report this ad1

If you need to upload the attachment as well as inserting it into the database, you should use [`media_handle_upload()`](https://developer.wordpress.org/reference/functions/media_handle_upload/), which will do all of that for you. All you have to do is give it the index of the file in the `$_FILES` array and the ID of the parent post:

```
$attachment_id = media_handle_upload( 'image', $post_id );
 
if ( is_wp_error( $attachment_id ) ) {
      // The upload failed.
} else {
      // The upload succeeded!
}
```

[Share](https://stackoverflow.com/a/22645444)[Improve this answer](https://stackoverflow.com/posts/22645444/edit)Follow[edited Jul 5 '20 at 20:53](https://stackoverflow.com/posts/22645444/revisions)
