Title: wp_insert_post categories array with variables
Last modified: August 20, 2016

---

# wp_insert_post categories array with variables

 *  [IshidaM](https://wordpress.org/support/users/ishidam/)
 * (@ishidam)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/wp_insert_post-categories-array-with-variables/)
 * This code works:
 *     ```
       $post_id = wp_insert_post( array(
       		'post_author'	=> $user_id,
                       'post_category' => array(3) )
       ```
   
 * //some stuff omitted
 * This does not
 *     ```
       $post_id = wp_insert_post( array(
       		'post_author'	=> $user_id,
                       'post_category' => array($category_id) )
       ```
   
 * Using echo, $category_id is indeed 3, so I don’t get why this is not working.
   I’m guessing this has something to do with php and array creation but I couldn’t
   figure out what is wrong.

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

 *  Thread Starter [IshidaM](https://wordpress.org/support/users/ishidam/)
 * (@ishidam)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/wp_insert_post-categories-array-with-variables/#post-2303898)
 * Anyone know whats wrong with the php code?
 *  [marsh_lion](https://wordpress.org/support/users/marsh_lion/)
 * (@marsh_lion)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/wp_insert_post-categories-array-with-variables/#post-2304034)
 * The PHP code you have written is creating an array with 3 elements in it, not
   an array with 1 element of 3.
 * PS. Yes I know this is an old query – I happened to stumble on it when searching
   for my issue with categories (and is something I find a pain as well)

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

The topic ‘wp_insert_post categories array with variables’ is closed to new replies.

## Tags

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

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [marsh_lion](https://wordpress.org/support/users/marsh_lion/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/wp_insert_post-categories-array-with-variables/#post-2304034)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
