I am trying to upload featured image from frontend and my id is also inserted in post_meta but image is not uploaded in media library
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 is name of input field. and cpt_id is post id.