0

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

2 Answers 2

1

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.

7
  • your-category means?
    – SANS780730
    Mar 30, 2012 at 19:11
  • @SANS780730 it's an example category slug.
    – soulseekah
    Mar 30, 2012 at 19:13
  • not working.can u edit my code perfectly.
    – SANS780730
    Mar 30, 2012 at 19:18
  • please can you edit my code correctly?
    – SANS780730
    Mar 30, 2012 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, 2012 at 19:21
-1

Check this.. this may help you 'toys' => 'Kids' = 'slug' => 'Catregory Name',

So if you can add your Category name here, it will assign that specific category to all your posts. is this what you required.

Check this for reference: https://codex.wordpress.org/Function_Reference/wp_set_post_categories

http://wordpress-code-snippets.blogspot.in/2014/05/how-to-assing-category-to-post.html

Copy and Paste the Below code in you theme functions.php.

function auto_add_category ($post_id = 0) {
 if (!$post_id) return;
 $tag_categories = array ( //  add multiple items as shown 'Tag Name' => 'Categroy Name'
   'toys' => 'Kids',
   'apple' => 'Fruits',
 );
 $post_tags = get_the_tags($post_id);
 foreach ($post_tags as $tag) {
   if ($tag_categories[$tag->name] ) {
     $cat_id = get_cat_ID($tag_categories[$tag->name]);
     if ($cat_id) {
       $result =  wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = 'category', $append = true );
     }
   }
   }
 }
 add_action('publish_post','auto_add_category');
  ?>
1
  • Please edit your answer, and add an explanation: why could that solve the problem? See How to Answer
    – fuxia
    Sep 22, 2016 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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