Title: does &#8220;wp_insert_post&#8221; insert post with a specific category ???
Last modified: August 18, 2016

---

# does “wp_insert_post” insert post with a specific category ???

 *  [raulurzua](https://wordpress.org/support/users/reus/)
 * (@reus)
 * [18 years, 9 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/)
 * I try toi use this code, but don’t work:
 * `// create object class`
    `class wm_mypost {`
 *     ```
       var $post_title;
       var $post_content;
       var $post_categories;
       var $post_status;
       var $post_author;
       }
       ```
   
 *     ```
       //// my first object :-)
       $wm_myobject = new wm_mypost();
       ```
   
 *     ```
       // fill object
       $wm_myobject->post_title = $title;
       $wm_myobject->post_content = $content;
       $wm_myobject->post_categories = $id_category;
       $wm_myobject->post_status = 'publish';
       $wm_myobject->post_author = $id_author;
       ```
   
 *     ```
       // feed object to wp_insert_post
       wp_insert_post($wm_myobject);
       ```
   
 * Please help me

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [pickledegg](https://wordpress.org/support/users/pickledegg/)
 * (@pickledegg)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614876)
 * Reus did you ever get to the bottom of this? I too am trying to insert a post
   into a specific category, and it always gos into uncategorized.
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614883)
 * I saw this example online somewhere too, but if you look at the code for wp_insert_post,
   it takes an array as input, not some class. This might work the way it’s written,
   but the error is that the post_category (not categories, category) field must
   be an array of strings, not a string.
 * If you fix the name and change the string to an array, maybe it will work. I 
   would tell you a better way to do it, but I haven’t got it working yet myself…
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614884)
 * Aha! My error was because I was inserting the post to a category that didn’t 
   exist. I need to figure out how to make a category now…
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614885)
 * Ok. It works and here’s the code:
 *     ```
       wp_create_category($cat_name); 	// Creates the category (if it doesn't already exist)
       ```
   
 * After that, change the Post->categories to post->category and it will insert 
   correctly. Now as to how to make it actually set the inserted post to the right
   category… I haven’t actually solved that yet.
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614886)
 * Nope that did it. Make sure the category exists and change to category from categories.
   It works perfectly for me now. YAY!
 *  [HairyPotter](https://wordpress.org/support/users/hairypotter/)
 * (@hairypotter)
 * [18 years, 2 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614891)
 * the full use of wp_create_categories is
 * wp_create_categories ($category_name, $post_id)
 * In this case, it will create a new category in the case the one you are trying
   to assing do not exist.
 * The problem is that this is theory and do not really works. Not for me.
 *  [chrislukenbill](https://wordpress.org/support/users/chrislukenbill/)
 * (@chrislukenbill)
 * [18 years, 2 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614892)
 * Thank you for this post!
 *  [jacobjbennett](https://wordpress.org/support/users/jacobjbennett/)
 * (@jacobjbennett)
 * [18 years ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614897)
 * jeremyduffy, can you post your code for this? did you end up having to insert
   the category as an array? How did you pass it in? a form? Thanks!
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614900)
 * Doh. Sorry for not checking back on this thread, but WordPress doesn’t alert 
   you to updates in a thread like OTHER SITES DO (/end rant).
 * I’ve posted the code that works here:
 * [http://www.jeremyduffy.com/computers-internet/wordpress-stuff/manually-insert-pagepost-into-wordpress-via-php/](http://www.jeremyduffy.com/computers-internet/wordpress-stuff/manually-insert-pagepost-into-wordpress-via-php/)
 *  [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614901)
 * Similar code can be found on the Codex, the page about [wp_insert_post](http://codex.wordpress.org/Function_Reference/wp_insert_post).
 *  [nmarquiess](https://wordpress.org/support/users/nmarquiess/)
 * (@nmarquiess)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614902)
 * What Are You Guys Talking About
 * Nicholas Marquiess

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘does “wp_insert_post” insert post with a specific category ???’ is closed
to new replies.

## Tags

 * [wp_insert_post](https://wordpress.org/support/topic-tag/wp_insert_post/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 11 replies
 * 8 participants
 * Last reply from: [nmarquiess](https://wordpress.org/support/users/nmarquiess/)
 * Last activity: [17 years, 11 months ago](https://wordpress.org/support/topic/does-wp_insert_post-insert-post-with-a-specific-category/#post-614902)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
