Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
What do you mean by trigger it for testing? – Sunyatasattva Apr 5 at 17:33

2 Answers

up vote 1 down vote accepted

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 );
share|improve this answer
How can I test this code? If making it a function, then where should I call it? – 10wtaylor Apr 7 at 16:25
I don't know. My point was to just let you know that you could update the post meta after however you are inserting the post. – helgatheviking Apr 7 at 18:47
Post thumbnail function will automatically find any meta data having image! – 10wtaylor Apr 9 at 9:01
That's not what you asked. You asked how to programmatically set a post thumbnail when inserting a post. You do that by setting the _thumbnail_id meta key for that post. If that's not what you meant, then please revise your question for clarity. – helgatheviking Apr 9 at 9:23
Then what's the use of update_post_meta function, if post thumbnail function couldn't retrieve image from it! – 10wtaylor Apr 9 at 11:09
show 2 more comments
 $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 http://codex.wordpress.org/Function_Reference/wp_insert_attachment

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.