I'm building a wordpress theme where people can submit posts using wp_insert_post. The code below adds the post title but does not add the category specified by the user. Instead it puts it in uncategorized. How do I get it to add the user submitted category on submit?
<?php
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$new_cat_ID = $_POST['category'];
//Checking if category already there
$cat_ID = get_cat_ID( $_POST['newcat'] );
//If not create new category
if($cat_ID == 0) {
$cat_name = array('cat_name' => $_POST['newcat']);
wp_insert_category($cat_name);
}
//Get ID of newly created category
$new_cat_ID = get_cat_ID($_POST['newcat']);
// Create post object
$new_post = array(
'ID' => '',
'post_title' => $post_title,
'post_status' => 'publish',
'post_author' => $user->ID,
'tax_input' => array( 'category' => $new_cat_ID )
);
// Insert the post into the database
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( home_url() ); exit;
}
?>
Heres the html form...
<form style="" action="" method="post" id="foo">
<input type="hidden" name="new_post" value="1"/>
<input type="text" name="post_title" value="http://<?php echo $seal; ?>" id="input-title"/>
<input type="text" name="post_category" value="2" id="post_category" />
<input type="submit" value="Login">
</form>
esc_*()
functions (see Codex) to make your user input a little more save.