$newpost = wp_insert_post($post_array);
if (!function_exists(‘wp_generate_attachment_metadata’)){
require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file][‘error’] !== UPLOAD_ERR_OK) {
return “upload error : ” . $_FILES[$file][‘error’];
}
$attachid = media_handle_upload( $file, $newpost );
}
}
if ($attachid > 0){

update_post_meta($newpost,’_thumbnail_id’,$attachid);
}

Your Answer
Sign In
Enter email and password to comment or answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.