我正在尝试从前端上传特色图像,我的id也会插入post\\u meta,但图像不会上传到媒体库
https://www.screencast.com/t/Uakjtxi9ojv
Here is my code.
$uploaddir = wp_upload_dir();
$file = $_FILES[\'cptFeatured\'];
$uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file[\'name\'] );
move_uploaded_file( $file["tmp_name"] , $uploadfile );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename[\'name\']), null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'menu_order\' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile, $cpt_id );
update_post_meta($cpt_id, \'_thumbnail_id\', $attach_id);
set_post_thumbnail( $cpt_id, $attach_id );
cptFeatured是输入字段的名称。cpt\\U id是post id。