How to post with categories to Wordpress using WP REST API?
Asked Answered
P

1

5

I created this small script to create wordpres posts, using Basic Auth, and it works. The problem is when I try to assign multiple categories to a post.

The reference is pretty vague. It says that the categories field must be an array. But it doesn't specify if it should be an array of category objects or if the id of these categories must be passed to the field. https://developer.wordpress.org/rest-api/reference/posts/#schema-categories

So I tried to make it fail so I can get more info from an exception message. The exception message says something like categories[0] is not an integer So I tried with a list of integers. And then, it works. But only one category is assigned, only the last category in the list.

So, How do I add more categories to a post ?

N1: Categories with id's 13 and 16 actually exists in my wordpress install.

N2: I know that I could create a draft, then create new requests to create categories then, use an update post endpoint to assign categories to posts... But in theory, should be possible to pass multiple categories just creating the post, since its in the reference xd

N3: I don't care about security. It is not a requirement.

import base64
import requests

r = requests.session()

wp_host = 'wphost.dev'
wp_username = 'FIXME'
wp_password = 'FIXME'

# BUILD BASIC AUTH STRING
basic_auth = str(
    base64.b64encode('{user}:{passwd}'.format(
        user=wp_username,
        passwd=wp_password
    ).encode()
), 'utf-8')

# PARAMETERS TO POST REQUEST
p = {
    'url': 'http://{wp_host}/wp-json/wp/v2/posts'.format(wp_host=wp_host),
    'headers': {'Authorization': 'Basic {basic_auth}'.format(basic_auth=basic_auth)},
    'data': {
        'title': 'My title',
        'content': 'My content',
        'categories': [13, 16],
        'status': 'publish',
    },
}

# THE REQUEST ITSELF
r = r.post(url=p['url'], headers=p['headers'], data=p['data'])

# Output
print(r.content)
# ... "categories":[16],"tags":[] ...
Parmenides answered 28/4, 2017 at 10:13 Comment(3)
Till I understand how to use multiple taxonomies with WP REST API. I'll use xmlrpc as a workaround. Using something like: gist.github.com/jeflopo/a1143e4dda496443c95f218f5bf6ae39Parmenides
I can't install libraries, How to do that?Showdown
@ehsanshirzadi If u mean base64 and requests these are python libraries, so you need to install python and then from command line use pip install <library_name>Parmenides
P
13

The WP API reference is misleading.

Actually comma separated string with categories IDs is expected:

data: {
    ...
    categories: "162,224"
    ...
}
Philippine answered 31/7, 2018 at 0:42 Comment(3)
Oh my god, how would anyone ever know this!!! Makes no sense, why a string? I guess maybe the values from select element are strings? -_-Yeanling
@OctaviaLo, right they should fix their API referencePhilippine
So annoying, I had the same problem... But your comment helped me @Samuel, thanks!Tipster

© 2022 - 2024 — McMap. All rights reserved.