Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer 1

up vote 1 down vote accepted

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

http://codex.wordpress.org/Function_Reference/wp_insert_post#Parameters

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
show 2 more comments

Your Answer

 
discard

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.