WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


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']);

//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">
share|improve this question
Please use the esc_*() functions (see Codex) to make your user input a little more save. – kaiser Jul 8 '13 at 10:52

I did not actually test your code thoroughly, but on the first glimpse:
your form input is named post_category while you're grabbing $_POST['new_cat']. Adjusting that (e.g., set both to post_category) should already do.

And why are you grabbing $_POST['category'] in line 5?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.