最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding post thumbnail in programatically inserted post

programmeradmin0浏览0评论

I want to insert post programatically so here is the code to add one:

global $user_ID;
$new_post = array(
    'post_title' => 'My New Post',
    'post_content' => 'Lorem ipsum dolor sit amet...',
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'post',
    'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

How to add a featured image to post and trigger it for testing ?

I want to insert post programatically so here is the code to add one:

global $user_ID;
$new_post = array(
    'post_title' => 'My New Post',
    'post_content' => 'Lorem ipsum dolor sit amet...',
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'post',
    'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

How to add a featured image to post and trigger it for testing ?

Share Improve this question asked Apr 5, 2013 at 17:03 10wtaylor10wtaylor 3151 gold badge4 silver badges13 bronze badges 1
  • What do you mean by trigger it for testing? – Sunyatasattva Commented Apr 5, 2013 at 17:33
Add a comment  | 

3 Answers 3

Reset to default 7

The post thumbnail is just saved as post meta with the key: _thumbnail_id. So after you insert the post and get the post id, you can set the post meta for that post. The $thumbnail_id is just the ID of the image you'd like to set as the thumbnail, up to you since I can't tell from your question what this should be.

global $user_ID;
$new_post = array(
    'post_title' => 'My New Post',
    'post_content' => 'Lorem ipsum dolor sit amet...',
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'post',
    'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

if( ! is_wp_error( $post_id ) )
       update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );

use the function set_post_thumbnail,

//$file is the path to your uploaded file (for example as set in the $_FILE posted file array)
//$filename is the name of the file
//first we need to upload the file into the wp upload folder. 
$upload_file = wp_upload_bits($filename, null, @file_get_contents($file));
if(!$upload_file['error']) {
  //if succesfull insert the new file into the media library (create a new attachment post type)
  $wp_filetype = wp_check_filetype($filename, null );
  $attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_parent' => $post_id,
    'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
    'post_content' => '',
    'post_status' => 'inherit'
  );
  //wp_insert_attachment( $attachment, $filename, $parent_post_id );
  $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );
  if (!is_wp_error($attachment_id)) {
     //if attachment post was successfully created, insert it as a thumbnail to the post $post_id
     require_once(ABSPATH . "wp-admin" . '/includes/image.php');
     //wp_generate_attachment_metadata( $attachment_id, $file ); for images
     $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
     wp_update_attachment_metadata( $attachment_id,  $attachment_data );
     set_post_thumbnail( $post_id, $attachment_id );
   }
}
 $wp_filetype = wp_check_filetype(basename($filename), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
     'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 
     'post_mime_type' => $wp_filetype['type'],
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
     'post_content' => '',
     'post_status' => 'inherit'
  );
  $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );     
  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 );

For more information see this link https://developer.wordpress/reference/functions/wp_insert_attachment/

发布评论

评论列表(0)

  1. 暂无评论