我现在已经可以使用了,在发布这个问题之前,我已经尝试了下面的代码,但是我丢失了要分配给它的帖子ID,所以我刚刚意识到并修复了它,现在它确实可以使用了!
以下是代码,以防将来对其他人有所帮助。。。
$photo_name = \'testimagename\';
$newId = $post_id;
// Set Featured Image
$photo = new WP_Http();
$photo = $photo->request( $image_url );
$attachment = wp_upload_bits( $photo_name . \'.jpg\', null, $photo[\'body\'], date("Y-m", strtotime( $photo[\'headers\'][\'last-modified\'] ) ) );
$filetype = wp_check_filetype( basename( $attachment[\'file\'] ), null );
$postinfo = array(
\'post_mime_type\' => $filetype[\'type\'],
\'post_title\' => $title . \' \',
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
);
$filename = $attachment[\'file\'];
$attach_id = wp_insert_attachment( $postinfo, $filename, $newId );
if( !function_exists( \'wp_generate_attachment_data\' ) )
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 );
set_post_thumbnail($post_id,$attach_id);