Я думаю, что вы ищете set_post_thumbnail (). Вот хороший учебник здесь , на котором вы можете основывать свой код.
Выдержка с соответствующим кодом:
<?PHP
//prepare upload image to WordPress Media Library
$upload = wp_upload_bits( $IMGFileName, null, file_get_contents( $IMGFilePath, FILE_USE_INCLUDE_PATH ) );
// check and return file type
$imageFile = $upload['file'];
$wpFileType = wp_check_filetype( $imageFile, null );
// Attachment attributes for file
$attachment = array(
'post_mime_type' => $wpFileType['type'], // file type
'post_title' => sanitize_file_name( $imageFile ), // sanitize and use image name as file name
'post_content' => '', // could use the image description here as the content
'post_status' => 'inherit'
);
// insert and return attachment id
$attachmentId = wp_insert_attachment( $attachment, $imageFile, $postId );
// insert and return attachment metadata
$attachmentData = wp_generate_attachment_metadata( $attachmentId, $imageFile );
// update and return attachment metadata
wp_update_attachment_metadata( $attachmentId, $attachmentData );
// finally, associate attachment id to post id
$success = set_post_thumbnail( $postId, $attachmentId );
// was featured image associated with post?
if ( $success ) {
$message = $IMGFileName . ' has been added as featured image to post.';
} else {
$message = $IMGFileName . ' has NOT been added as featured image to post.';
}
?>