Title: Creating post with category doesn&#039;t work
Last modified: August 31, 2016

---

# Creating post with category doesn't work

 *  Resolved [shembelcut](https://wordpress.org/support/users/shembelcut/)
 * (@shembelcut)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/)
 * I am trying to add a new post through the api, but the category part doesn’t 
   work.
    based on documentation I need to pass the ‘categories’ arguments as an
   array object and so far I have tried all of these requests :
 * ‘title’ => ‘{‘test’}’,
    ‘categories’ => ‘array(13)’,
 * ————————-
    ‘title’ => ‘{‘test’}’, ‘categories’ => ‘[13]’,
 * ————————-
    ‘title’ => ‘{‘test’}’, ‘categories’ => [ 0 => 13 ],
 * but none of them is working and I only get the post with ‘test’ title and the
   default category (not with the expected category)
 * I would really appreciate if you could help me with this matter.
 * [https://wordpress.org/plugins/rest-api/](https://wordpress.org/plugins/rest-api/)

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

 *  [ardend](https://wordpress.org/support/users/ardend/)
 * (@ardend)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/#post-7186053)
 * I replied to another recent post about this same issue. It seems that creating
   or editing Tag and Categories isn’t working properly. Every format of passing
   the arguments seems always result in the same error in the response:
 *     ```
       <b>Warning</b>:  array_map(): Argument #2 should be an array in <b>/sitename/wp-content/plugins/rest-api/lib/endpoints/class-wp-rest-posts-controller.php</b> on line <b>918</b><br />
       ```
   
 * Hoping for a fix or any info soon as creating a post without a category is going
   to be an issue for most I would assume. I know the team is slammed I’m sure working
   towards their integration into WP, but hopefully we can find out how to fix or
   get around this.
 *  Plugin Author [Daniel Bachhuber](https://wordpress.org/support/users/danielbachhuber/)
 * (@danielbachhuber)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/#post-7186176)
 * I’m not able to reproduce the issue in my testing.
 * [@shembelcut](https://wordpress.org/support/users/shembelcut/) Can you share 
   the full request you’re making?
 * > Every format of passing the arguments seems always result in the same error
   > in the response:
 * [@ardend](https://wordpress.org/support/users/ardend/) Can you share the request
   you’re making? `categories` is meant to be passed as an array — if you don’t 
   pass the argument as an array, you’ll get that PHP error.
 *  [ardend](https://wordpress.org/support/users/ardend/)
 * (@ardend)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/#post-7186177)
 * Hi Daniel,
 * Here is the code I am using to send the request, which looks right but the param
   for categories seem to get messed up when sent as you’ll see below:
 *     ```
       $scope.formData.categories = [3, 8];
   
       			$http({
       				method: 'POST',
       				url: ($scope.api + 'wp/v2/posts/32'),
       				params: $scope.formData,
       				headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': RestAPI.nonce }
       			})
       			.success(function (result) {
       				console.log('Success!');
       			}).error(function () {
       				console.log('Fail!');
       			});
       ```
   
 * And here is what happens with the request when i pass it like that:
 *     ```
       http://sitename/wp-json/wp/v2/posts/32?categories=3&categories=8
       ```
   
 * This simply removes any categories in wordpress associated with the post.
 * When i try and pass it like this: $scope.formData.categories = “[3, 8]”;
 * The request looks like this:
 *     ```
       http://sitename/wp-json/wp/v2/posts/32?categories=%5B3,+8%5D
       ```
   
 * The request looks right, but all this does is set the post to uncategorized. 
   Any help would be great thanks!
 *  Plugin Author [Daniel Bachhuber](https://wordpress.org/support/users/danielbachhuber/)
 * (@danielbachhuber)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/#post-7186181)
 * In both cases, your request is passing `categories` as a string, which means 
   it’s not being interpreted correctly.
 * Here’s how you would pass the first as array data:
 * `http://sitename/wp-json/wp/v2/posts/32?categories[]=3&categories[]=8`
 * Note the square brackets.
 * Here’s how you’d pass the second as array data:
 * `$scope.formData.categories = [3, 8];`
 * Note I’ve removed the double quotes.
 *  [ardend](https://wordpress.org/support/users/ardend/)
 * (@ardend)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/#post-7186184)
 * Actually in the first line of code i set the value to an array like you mentioned
   for the second example, only when its actually sent it splits it up like you 
   see (32?categories=3&categories=8). Is it the headers maybe?
 * If it helps, formData is initialized as an empty json to start:
 * $scope.formData = {};
 * I feel like there is some specific combination of how to format the param and
   how to pass it that I am missing.
 *  [ardend](https://wordpress.org/support/users/ardend/)
 * (@ardend)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/#post-7186187)
 * Ok got this sorted out. For anyone else using Angular, apparently Angular’s HTTP
   post serializes data in a way that php won’t normally parse correctly, so you
   have to modify the headers in Angular. I found this on stackoverflow:
 * [http://stackoverflow.com/questions/12190166/angularjs-any-way-for-http-post-to-send-request-parameters-instead-of-json](http://stackoverflow.com/questions/12190166/angularjs-any-way-for-http-post-to-send-request-parameters-instead-of-json)
 * Check out the answer from Thomas Graziani, about 4 or so down the page. Besides
   the transform part added to the app.js file, changing the data part of the request
   code from “params” to “data” made it work. Hope this helps!
 *  [jg88](https://wordpress.org/support/users/jg88/)
 * (@jg88)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/#post-7186294)
 * Just the other day i came with the same error **array_map(): Argument #2 should
   be an array**.For some reasons my posted categories are still in json format 
   and so php can’t parse them.
    Note: I’m working with a c# client app, so it’s
   out of context. I did some modification in /lib/endpoints/class-wp-rest-posts-
   controller.php Line No around 938 the function handle_terms as:
 * `
    protected function handle_terms( $post_id, $request ) { $taxonomies = wp_list_filter(
   get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' =
   > true ) ); foreach ( $taxonomies as $taxonomy ) { $base = ! empty( $taxonomy-
   >rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
 *  if ( ! isset( $request[ $base ] ) ) {
    continue; } $arr = $request[ $base ];
   if (!is_array($arr)) { $arr = json_decode($request[ $base ]); } $terms = array_map('
   absint', $arr); $result = wp_set_object_terms( $post_id, $terms, $taxonomy->name);
   if ( is_wp_error( $result ) ) { return $result; } } }
 * * Just json decoding the $result[base] if not it’s an php array.*

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

The topic ‘Creating post with category doesn't work’ is closed to new replies.

 * ![](https://ps.w.org/rest-api/assets/icon-256x256.png?rev=1346297)
 * [WordPress REST API (Version 2)](https://wordpress.org/plugins/rest-api/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/rest-api/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/rest-api/)
 * [Active Topics](https://wordpress.org/support/plugin/rest-api/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/rest-api/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/rest-api/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [category](https://wordpress.org/support/topic-tag/category/)

 * 7 replies
 * 4 participants
 * Last reply from: [jg88](https://wordpress.org/support/users/jg88/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/creating-post-with-category-doesnt-work/#post-7186294)
 * Status: resolved