skip to Main Content

I am sending posts with wordpress rest api. But I can’t send meta description. where did i go wrong.

wp_api_url = "https://xxxxx.com/wp-json/wp/v2/posts"
wp_auth_token = "xxxxxxx"

import requests
meta = {
    "description": "test description",
    "keywords": "test keywords",
    "tags": "test Tag"
}

data = {
    "title": conttitle,
    "content": contcontent,
    "status": "publish",
    "data": {"filter": "raw", "meta": meta}

        }

# Set headers for WordPress REST API request
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {wp_auth_token}",
}
response = requests.post(wp_api_url, headers=headers, json=data)

2

Answers


  1. The arguments available to create a core WordPress Post are listed here. The argument data is not listed there (and may not be valid). meta and tags are top level arguments. meta is not a property of data. tags (Post tags) is not a property of meta.

    Try rearranging the json payload as follows:

    json payload

    meta = {
        "description": "test description",
        "keywords": "test keywords",
        "filter": "raw" // This might not apply. See note under 'filter' below.***
    }
    
    data = {
        "title": conttitle,
        "content": contcontent,
        "status": "publish",
        "tags": "test Tag",
        "meta": meta
    }
    

    filter

    ***The filter value might not apply to your case. You could be thinking of the ‘context’ argument used when sanitizing Posts.

    custom post type

    If trying to save meta data for a custom post type, please see the answer to this question: how to create a post having a custom field meta value via the WP REST API.

    Login or Signup to reply.
  2. Use the Rest API wp/v2/posts/{id} endpoint with the below data:

    data =  {
      "title": conttitle,
      "content": contcontent,
      "status": "publish",
      "meta": {
        "description": "This is the meta description of the post."
      }
    }
    

    This will update the post meta description. also check https://developer.wordpress.org/rest-api/reference/posts/#create-a-post

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search