<?PHP
// $imageFile - переменная которой нужно присвоить путь к файлу из произвольного поля
$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.';
}
*/
?>