Have a front-end form to create a post, Was only able to add text data to the new post (title, content, text-typed custom fields). I want to add a featured image to the new posts too.
It's a WordPress site and I use Avada theme. The custom post type is the Avada's default Portfolio post. But generalised answers would be very helpful too (please give some explanation on your code).
Please add some code to my PHP code and the form so that it can save a featured image too.
So, Here's my php code in fuctions.php in my child-theme:
if(isset($_POST['title'])){
$custom_field_address1 = $_POST['address1'];
$my_post = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['description'],
'post_status' => 'publish',
'post_type' => 'your_post_type_name',
'meta_input' => array(
'address1' => $custom_field_address1,
)
);
$post_id = wp_insert_post($my_post);
add_post_meta( $post_id, 'address1', $custom_field_address1, false );
echo 'New Post Saved !';
die;
}
My front-end form:
<form method="post">
<div class="form-group">
<label for="title">Post Title:</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group">
<label for="pwd">Post Description :</label>
<textarea class="form-control" name="description"></textarea>
</div>
<div class="form-group">
<label for="address1">Address :</label>
<input type="text" name="address1" id="address1">
</div>
<BR>
<button type="submit">Submit</button>
</form>