How to add featured images from front-end?


I have a code that creates a custom post type from front-end form. But it only saves text data like title and the content. What should I add to this code to be able to save images too?

P.S I can’t use plugins like Post My CF7 Form. They didn’t work on my website. I have to write code.

///How to submit custom post type in wordpress from the Front-End


//echo $_POST['title']; // print title variable value

// create post object

$custom_field_add1 = $_POST['add1'];
$custom_field_phone = $_POST['phone'];

$my_post = array(

'post_title' => $_POST['title'],
'post_content' => $_POST['description'],
'post_status' => 'publish', 
'post_type' => 'companies',
'meta_input' => array(
    'add1' => $custom_field_add1,
    'phone' => $custom_field_phone,

// i use wordpres predefine function/
$post_id = wp_insert_post($my_post);

add_post_meta( $post_id, 'add1', $custom_field_add1, false );
add_post_meta( $post_id, 'phone', $custom_field_phone, false );
echo 'New Post Saved !';

die; // stop script after form submit
Davood Kazemi 2 months 0 Answers 17 views 0

Leave an answer