Here is some code that I've used to add posts from a front end form that also adds meta data and taxonomy terms. Note that snippet extracts all of the security, data validation and data sanitization. It just show what I do with the data to add it to the database once I've sanitized it all.
// Submit the values if there are no errors
if(empty($errors))
{
// Prepare title
$term = get_term_by('id', $values['complaint_type'], 'complaint-type');
$title = $values['address_clean'].' ('.$term->name.')';
// Gather post data
$post = array(
'post_title' => $title,
'post_content' => $values['description'],
'post_status' => 'publish',
'post_type' => PREFIX_POST_TYPE_NAME,
'post_author' => 1
);
// Attempt to add post
if($id = wp_insert_post($post))
{
// Add metadata to post
update_post_meta($id, '_'.PREFIX_PLUGIN_NAME_L.'_latitude', $values['latitude']);
update_post_meta($id, '_'.PREFIX_PLUGIN_NAME_L.'_longitude', $values['longitude']);
// Associate complaint-type
if(!wp_set_post_terms($id, $values['complaint_type'], 'complaint-type'))
$errors['wp_set_post_terms'] = 'There was an error adding the complaint type.';
}
else
$errors['wp_insert_post'] = 'There was an error adding the complaint.';
}