php - Programattically adding Wordpress post with attachment -


I get $ _REQUEST as well as post_ title, post_ content and other things in an image file. I am in WordPress database I want to save everyone as a post. On my page

  & lt ;? Php require_once ("wp-config.php"); $ User_id; // get it from my function $ post_title = $ _REQUEST ['post_title']; $ Post_content = $ _REQUEST ['post_content']; $ Post_cat_id = $ _REQUEST ['post_cat_id']; // File name id id = $ _FILES ['image'] ['name']; // I have all this one array $ postarr = array ('post_status' = & gt; 'publish', 'post_type' = & gt; 'post', 'post_title' = & gt; $ post_title, 'post_content' = & gt; ; $ Post_content, 'post_author' = & gt; $ user_ID, 'post_category' => Array ($ category); $ Post_id = wp_insert_post ($ postarr); ? & Gt;  

It will get everything in the database as a post, but I do not know how to add attachments and its meta.

How can I do this? Can anybody help me? I am really confused and have been trying to solve it for a few days.

To add an attachment, use Wp_inert_attachment ():

Example:

  & lt ;? Php $ wp_filetype = wp_check_filetype (bnname ($ filename), null); $ Attachment = array ('post_mime_type' = & gt; $ wp_filetype ['type'], 'post_title' = & gt; preg_replace ('/.[[.]+$/', '', bassname ($ filename)) , 'Post_sign' => gt; '', 'post_status' = & gt; 'heirs'); $ Attach_id = wp_inert_attachment ($ attachment, $ filename, 37); // You must first include the wp_generate_attachment_metadata () function for the image.php file //. (Aspation. "Wp-admin". '/ Include /image.php'); $ Attach_data = wp_generate_attachment_metadata ($ attach_id, $ filename); Wp_update_attachment_metadata ($ attach_id, $ attach_data); ? & Gt;  

To add meta data, use wp_update_attachment_metadata ():

  & lt; Php wp_update_attachment_metadata ($ post_id, $ data)? & Gt;  

Comments