首页 > 解决方案 > Python 到 wordpress

问题描述

我已经编写了这段代码,用于将带有元数据的 cpt 从 python 发送到 WordPress,这是我已经证明的 4 种不同场景之一,实际上我需要使用外部变量将带有一些元字段的帖子从 python 发送到 WordPress设置标题和产品价格和产品规格,我需要通过获取请求向邮递员请求的 JSON 文件是

[
    {
        "id": 3693,
        "date": "2021-07-14T20:00:35",
        "date_gmt": "2021-07-14T18:00:35",
        "guid": {
            "rendered": "http://www.mydomain.it/acme_product/spec-3/"
        },
        "modified": "2021-07-20T01:58:48",
        "modified_gmt": "2021-07-19T23:58:48",
        "slug": "spec-3",
        "status": "publish",
        "type": "acme_product",
        "link": "http://www.mydomain.it/acme_product/spec-3/",
        "title": {
            "rendered": "spec"
        },
        "featured_media": 0,
        "template": "",
        "product_price": null,
        "product_spec": null,
        "_links": {
            "self": [
                {
                    "href": "http://www.mydomain.it/wp-json/wp/v2/acme_product/3693"
                }
            ],
            "collection": [
                {
                    "href": "http://www.mydomain.it/wp-json/wp/v2/acme_product"
                }
            ],
            "about": [
                {
                    "href": "http://www.mydomain.it/wp-json/wp/v2/types/acme_product"
                }
            ],
            "wp:attachment": [
                {
                    "href": "http://www.mydomain.it/wp-json/wp/v2/media?parent=3693"
                }
            ],
            "curies": [
                {
                    "name": "wp",
                    "href": "https://api.w.org/{rel}",
                    "templated": true
                }
            ]
        }
    },

当我实际使用我的 python 代码 wordpress 接受该帖子但不填写产品价格和产品规格的字段时,一些想法,我该怎么办?谢谢大家

from __future__ import print_function
import base64
import sys
from datetime import datetime
import json
import requests

user= 'username'
password='auth
url = 'http://www.mydomain.it/wp-json/wp/v2/'
d= sys.argv
a_dict={}
b_dict={}

data_string = user + ':' + password
token = base64.b64encode(data_string.encode())
headers = {'Authorization': 'Basic ' + token.decode('utf-8')}
date="2021-07-14T20:00:35"
title=d[1]
slug= d[1]
status= "publish"
author="1"
##excerpt="exc post"
##format="standard"
product_price= d[2]
product_spec=d[3]
for var in ["product_price","product_spec"]:
    b_dict[var]= eval(var)
    print (b_dict[var])
meta= b_dict[var]
for variable in ["date", "title", "slug","status","author","product_price","product_spec"]:
    a_dict[variable] = eval(variable)

r = requests.post(url + 'acme_product', headers=headers, json=a_dict)

print(r.content.decode('utf-8'))
print('Your post is published on ' + json.loads(r.content.decode('utf-8'))['link'])

标签: pythonwordpress

解决方案


推荐阅读