Problem with send metadata from python to wordpress

Viewed 91

i tried to send post from python requests to wordpress and every thing is good except one thing, when i send the metadata its not sending the value of the key !

what i tried to in code , Firstly need to nonce Authentication key:

def get_nonce(wp_url, username, password, session):
    wp_login = f"{wp_url}/wp-login.php"
    wp_redirect = f"{wp_url}/wp-admin/post-new.php"
    login_data = {
        'log': username, 'pwd': password, 'wp-submit': 'Log In',
        'redirect_to': wp_redirect
    }
    wp = wp_session.post(wp_login, data=login_data,allow_redirects=False)
    wp = wp_session.get(wp_redirect)
    wp_nonce = re.findall('var wpApiSettings = .*\;', wp.text)
    wp_nonce = re.sub('^.*\"nonce\"\:\"', '', wp_nonce[0])
    wp_nonce = re.sub('\".*$', '', wp_nonce)
    print(f"Done found the wp_nonce :{wp_nonce}")
    return wp_nonce

now we have a nonce for POST new post on wordpress:

fields = {
"title":"Hello from python",
"status":"publish",
"content":"Hey this is my content of the post from python script",
"metadata":{"_meta_key":"the value of the meta"}
}
wp_session = requests.session()
wp_url = "http://example.com"
nonce = get_nonce(wp_url,"admin", "password", wp_session)
header = {
    "X-WP-Nonce":nonce
}
send = wp_session.post(f"{wp_url}/wp-json/wp/v2/posts",params=fields,headers=header)
print(send.text)

and finally i got a post with empty value of key as you can see here in the wp_postmeta in phpmyadmin: The problem

0 Answers
Related