how to insert photo using wp_insert_post

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 want to set the post as a featured Image

Related posts

Leave a Reply

1 comment

  1. 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);