我在$_REQUEST中获得post_title,post_content和其他东西以及图像文件.我想将所有这些保存为wordpress数据库中的帖子.我在我的页面上
<?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); ?>
这会将数据库中的所有内容都作为post发送,但我不知道如何添加附件及其post Meta.
我怎样才能做到这一点?有谁能够帮我?我真的很困惑,并花了几天时间试图解决这个问题.
要添加附件,请使用wp_insert_attachment():
http://codex.wordpress.org/Function_Reference/wp_insert_attachment
例:
<?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 ); ?>
要添加元数据,请使用wp_update_attachment_Metadata():
http://codex.wordpress.org/Function_Reference/wp_update_attachment_metadata
<?PHP wp_update_attachment_Metadata( $post_id,$data ) ?>