Title: wp_insert_post with category variable not working
Last modified: August 20, 2016

---

# wp_insert_post with category variable not working

 *  [mittul.agile](https://wordpress.org/support/users/mittulagile/)
 * (@mittulagile)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/wp_insert_post-with-category-variable-not-working/)
 * hi .. i m encountering a strange problem in which i m inserting a post using 
   the category variable .. here below is my working code ..
 * ————————————
    $new_post = array( ‘post_title’ => $title, ‘post_content’ => $description,‘
   post_date’ => $post_date, ‘post_status’ => ‘future’, ‘post_category’ => array(
   18,19,20), );
 *  //SAVE THE POST
    $pid = wp_insert_post($new_post); ————————————
 * this code works fine .. however when i try to add categories into an array with
   variable .. then it is only adding first category value into the post ..
 * ————————————
    $new_array = array(18,19,20); $final_cat_id = implode(‘,’,$new_cat_ID);
   echo $final_cat_id;
 *  $title = ‘title’;
    $description = ‘description’; $post_date = date(“Y-m-d H:
   i:s”);
 *  // Create post object
 *  $new_post = array(
    ‘post_title’ => $title, ‘post_content’ => $description, ‘
   post_date’ => $post_date, ‘post_status’ => ‘future’, ‘post_category’ => array(
   $final_cat_id), );
 *  //SAVE THE POST
    $pid = wp_insert_post($new_post); ————————————
 * i m getting 18,19,20 when i m echoing the $final_cat_id but it is only adding
   18 category when inserting a post ..
 * can someone help me where i am going wrong . .

Viewing 1 replies (of 1 total)

 *  Thread Starter [mittul.agile](https://wordpress.org/support/users/mittulagile/)
 * (@mittulagile)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/wp_insert_post-with-category-variable-not-working/#post-3480338)
 * ok . i have sorted out my problem by not imploding it ..
 * just do this
 * ‘post_category’ => $new_array,
 * remove the lines
 * $final_cat_id = implode(‘,’,$new_cat_ID);
    echo $final_cat_id;
 * as it will automatically imploding while inserting a post .. cheers !

Viewing 1 replies (of 1 total)

The topic ‘wp_insert_post with category variable not working’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [mittul.agile](https://wordpress.org/support/users/mittulagile/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/wp_insert_post-with-category-variable-not-working/#post-3480338)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
