Support » Plugin: WordPress REST API (Version 2) » How to create a post with category?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    There are some examples on the documentation website. In short, you need to include an array of category term ids in the request body with key “categories”: categories: [ 3, 5 ]

    Can you share the requests you’ve tried, and the error response you receive?

    Thanks for the feedback , I am sending this way you went , but it is not working.

    Follows the requested information:

    categories:

    [
    
    {
    
        "id": ​1,
        "count": ​0,
        "description": "",
        "link": "http://localhost/wordpress/category/sem-categoria/",
        "name": "Sem categoria",
        "slug": "sem-categoria",
        "taxonomy": "category",
        "parent": ​0,
        "_links": 
    
    {
    
        "self": 
    
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories/1"
        }
    
    ],
    "collection":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories"
        }
    
    ],
    "about":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/taxonomies/category"
        }
    
    ],
    "http://api.w.org/v2/post_type":
    [
    
                {
                    "href": "http://localhost/wordpress/wp-json/wp/v2/posts?categories=1"
                }
            ]
        }
    
    },
    {
    
        "id": ​2,
        "count": ​0,
        "description": "",
        "link": "http://localhost/wordpress/category/teste/",
        "name": "teste",
        "slug": "teste",
        "taxonomy": "category",
        "parent": ​0,
        "_links": 
    
    {
    
        "self": 
    
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories/2"
        }
    
    ],
    "collection":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories"
        }
    
    ],
    "about":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/taxonomies/category"
        }
    
    ],
    "http://api.w.org/v2/post_type":
    [
    
                {
                    "href": "http://localhost/wordpress/wp-json/wp/v2/posts?categories=2"
                }
            ]
        }
    
    },
    {
    
        "id": ​3,
        "count": ​0,
        "description": "",
        "link": "http://localhost/wordpress/category/teste2/",
        "name": "teste2",
        "slug": "teste2",
        "taxonomy": "category",
        "parent": ​0,
        "_links": 
    
    {
    
        "self": 
    
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories/3"
        }
    
    ],
    "collection":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories"
        }
    
    ],
    "about":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/taxonomies/category"
        }
    
    ],
    "http://api.w.org/v2/post_type":
    [
    
                {
                    "href": "http://localhost/wordpress/wp-json/wp/v2/posts?categories=3"
                }
            ]
        }
    
    },
    {
    
        "id": ​5,
        "count": ​0,
        "description": "",
        "link": "http://localhost/wordpress/category/teste3/",
        "name": "teste3",
        "slug": "teste3",
        "taxonomy": "category",
        "parent": ​0,
        "_links": 
    
    {
    
        "self": 
    
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories/5"
        }
    
    ],
    "collection":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories"
        }
    
    ],
    "about":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/taxonomies/category"
        }
    
    ],
    "http://api.w.org/v2/post_type":
    [
    
                {
                    "href": "http://localhost/wordpress/wp-json/wp/v2/posts?categories=5"
                }
            ]
        }
    
    },
    {
    
        "id": ​4,
        "count": ​0,
        "description": "",
        "link": "http://localhost/wordpress/category/teste4/",
        "name": "teste4",
        "slug": "teste4",
        "taxonomy": "category",
        "parent": ​0,
        "_links": 
    
    {
    
        "self": 
    
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories/4"
        }
    
    ],
    "collection":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/categories"
        }
    
    ],
    "about":
    [
    
        {
            "href": "http://localhost/wordpress/wp-json/wp/v2/taxonomies/category"
        }
    
    ],
    "http://api.w.org/v2/post_type":
    [
    
                    {
                        "href": "http://localhost/wordpress/wp-json/wp/v2/posts?categories=4"
                    }
                ]
            }
        }
    
    ]

    Code request:

    import requests
    from requests.auth import HTTPBasicAuth
    
    payload = {'title': 'teste', 'content': 'calixto', 'categories': [1, 2, 3, 4, 5]}
    
    r = requests.post('http://localhost/wordpress/wp-json/wp/v2/posts/', auth=HTTPBasicAuth('', ''), params=payload)
    
    print(r.text)

    Response:

    <br />
    <b>Warning</b>:  array_map(): Argument #2 should be an array in <b>/opt/lampp/htdocs/wordpress/wp-content/plugins/rest-api/lib/endpoints/class-wp-rest-posts-controller.php</b> on line <b>918</b><br />
    {
       "id":91,
       "date":"2016-02-26T03:36:10",
       "date_gmt":null,
       "guid":{
          "rendered":"http:\/\/localhost\/wordpress\/?p=91",
          "raw":"http:\/\/localhost\/wordpress\/?p=91"
       },
       "modified":"2016-02-26T03:36:10",
       "modified_gmt":null,
       "password":"",
       "slug":"",
       "status":"draft",
       "type":"post",
       "link":"http:\/\/localhost\/wordpress\/?p=91",
       "title":{
          "raw":"teste",
          "rendered":"teste"
       },
       "content":{
          "raw":"calixto",
          "rendered":"<p>calixto<\/p>\n"
       },
       "excerpt":{
          "raw":"",
          "rendered":"<p>calixto<\/p>\n"
       },
       "author":1,
       "featured_media":0,
       "comment_status":"open",
       "ping_status":"open",
       "sticky":false,
       "format":"standard",
       "categories":[
    
       ],
       "tags":[
    
       ],
       "_links":{
          "self":[
             {
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/posts\/91"
             }
          ],
          "collection":[
             {
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/posts"
             }
          ],
          "about":[
             {
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/types\/post"
             }
          ],
          "author":[
             {
                "embeddable":true,
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/users\/1"
             }
          ],
          "replies":[
             {
                "embeddable":true,
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/comments?post=91"
             }
          ],
          "version-history":[
             {
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/posts\/91\/revisions"
             }
          ],
          "https:\/\/api.w.org\/attachment":[
             {
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/media?parent=91"
             }
          ],
          "https:\/\/api.w.org\/term":[
             {
                "taxonomy":"category",
                "embeddable":true,
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/categories?post=91"
             },
             {
                "taxonomy":"post_tag",
                "embeddable":true,
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/tags?post=91"
             }
          ],
          "https:\/\/api.w.org\/meta":[
             {
                "embeddable":true,
                "href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/posts\/91\/meta"
             }
          ]
       }
    }

    I cant get this to work either. Here is the javascript post I am trying to use to edit the categories of a post:

    $http({
    	method: 'POST',
    	url: ($scope.api + 'wp/v2/posts/32'),
    	params: JSON.stringify({'categories': [3,8,11]}),
    	headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': RestAPI.nonce }
    	})
    	.success(function (result) {
    		console.log('Success!');
    	}).error(function () {
    		console.log('Fail!');
    	});

    But this does nothing at all. The post sends successfully but there is no change in the categories for the post. Any ideas on how to modify the params above to the way the API is looking for?

    Thanks for any help!

    Just bumping this as I still can’t get any kind of category request to work. Any ideas? I know the team is swamped, so any help or update is appreciated, thanks!

    {“title”:{“raw”:”Hello test”, “rendered”:”Hello test”},”content”:{“raw”:”jeey”, “rendered”:”Jeey”},”excerpt”: {“raw”: “Excerpt”,”rendered”: “Excerpt”},”categories”:[2,3]}

    Is working for me with postman.

    why not try

    {“title”:”Hello World!”,”content”:”Content”,”excerpt”:”Excerpt”}

    works with basic auth plugin

    Had this exact problem too. Figured it out though:

    data = { 'title': "Goodbye heart", 'status':'publish', 'content':"Hello Mary-Lou", 'categories[]': [42] }

    You need to add [] to the categories parameter. That way the request can build the approriate URL, I guess: http://sitename/wp-json/wp/v2/posts/32?categories[]=42

    Thanks @danielbachhuber ! I totally missed the schema before. Appreciate it!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to create a post with category?’ is closed to new replies.