Wednesday, 19 April 2023

Category doesn't get assigned to WordPress post in Python using REST API

I've created a python script to create & post articles to WordPress site but it seems that the category in post data isn't getting assigned to the posts and it always gets assigned to uncategorized category, I am looking to only assign 1 category to the posts. Am I doing something wrong here? WordPress REST API Docs aren't really helpful.

Here's the post creator function:

# Post creator function
def create_post(inputTitleSent, outputText):
    randomAuthor = random.choice(authorList)
    
    post_status = "draft"
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    post = {
        "title": inputTitleSent,
        "content": outputText,
        "status": post_status,
        "author": randomAuthor,
        "categories:": "6"
    }
    url = wp_base_url + "/wp-json/wp/v2/posts"
    response = requests.post(url, data=post, headers=headers, auth=(wp_username,wp_password))
    return response

I've tried "categories": 6 and I saw somewhere that it's suppose to be an array so I tried "categories": [6] and "categories": ['6'] but still posts get assigned to uncategorized category.



from Category doesn't get assigned to WordPress post in Python using REST API

No comments:

Post a Comment