开发者

how to insert photo using wp_insert_post

开发者 https://www.devze.com 2023-03-26 08:40 出处:网络
my question is about using wp_insert_post to insert post with type \"photo\" not needs to upload the image right now just I want to insert the post information into the database

my question is about using wp_insert_post to insert post with type "photo" not needs to upload the image right now just I want to insert the post information into the database I used this code and it is working

$my_post = array(

     'post_title' => 'My post',
     'post_content' => 'This is my post.',
     'post_status' => 'publish',
     'post_author' => 1,
      'post_type' => 'photo',
     'post_category' => array(3)
  );

the question is I want to add the following informations 1- the photo type 2- I w开发者_运维问答ant to set the post as a featured Image


You first need to upload the file, then you can attach the image as an attachment to your post. This is the code I use to automate a blog:

$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
 'post_mime_type' => $wp_filetype['type'],
 'post_title' => $postTitle,
 'post_content' => '',
 'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename, $postId);
// 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号