How to programmatically assign a category to a post on WordPress

Another quick little snippet. This is when you use it in a loop.

    $my_post = array();
      $my_post['ID'] = $post->ID;

      $catarray = array(31); // 31 is the cat ID you want to assign

      foreach((get_the_category($post->ID)) as $category) { 
        array_push($catarray, $category->cat_ID);

      $my_post['post_category'] = $catarray;
      wp_update_post( $my_post );

So lazy that he can't even fill this column out.

© 2012 Suhas Tech. All rights reserved.
Proudly powered by Wordpress.