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 ?
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.use the function
set_post_thumbnail
,For more information see this link
https://developer.wordpress.org/reference/functions/wp_insert_attachment/