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

how to set category name for post: this is my code ,this is not working ,why?

    $post_title = $vname; 
    $categ='category name';
$post_content = '[newpage link="'.$videos.'"]';  
    $new_post = array(
      'ID' => '',
      'post_author' => $user->ID, 
      'post_category' => $categ,
      'post_content' => $post_content,
      'post_title' => $post_title,
      'post_status' => 'publish',

$post_id = wp_insert_post($new_post);
share|improve this question
up vote 1 down vote accepted

The post_category parameter has to be an array of IDs (int).


Try get_category_by_slug to get the ID, then use it.

$category = get_category_by_slug( 'your-category' );
$new_post = array(
       'post_category' => array( $category->term_id )

Alternatively, look into wp_set_object_terms or wp_set_post_terms, using the $post_id of the post you've just created.

If you need to add a category that is non-existent yet, create it using wp_create_category.

Good luck.

share|improve this answer
your-category means? – SANS780730 Mar 30 '12 at 19:11
@SANS780730 it's an example category slug. – soulseekah Mar 30 '12 at 19:13
not working.can u edit my code perfectly. – SANS780730 Mar 30 '12 at 19:18
please can you edit my code correctly? – SANS780730 Mar 30 '12 at 19:21
Are you serious? With 29% accepts, almost 30 questions and no answers, you expect me to "edit [your] code [to work] perfectly"!? Seriously?!.. You aren't putting ANY effort into your questions whatsoever, why should I or anybody else? – soulseekah Mar 30 '12 at 19:21

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.