skip to Main Content

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.

2

Answers


  1. So, Basically, there is a typo in your POST payload "categories:": "6" you can simply remove the : from the double quote

    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]  # Replace "categories:" with "categories"
        }
        url = wp_base_url + "/wp-json/wp/v2/posts"
        response = requests.post(url, data=post, headers=headers, auth=(wp_username,wp_password))
        return response
    
    Login or Signup to reply.
  2. You have an additional : character in the post payload under categories:

        post = {
            "title": inputTitleSent,
            "content": outputText,
            "status": post_status,
            "author": randomAuthor,
            "categories:": "6"
    #                  ^
        }
    

    Simply remove this and you’ll get the results you’re looking for:

    # 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search