【问题标题】:How to submit custom fields data by python wordpress xmlrpc如何通过 python wordpress xmlrpc 提交自定义字段数据
【发布时间】:2019-08-21 04:24:12
【问题描述】:

我在我的网站上使用 wp 作业管理器。当我尝试通过 xmlrpc 添加列表时,一切正常,但类别和位置为空。

Screenshot Screenshot

我的代码如下。你能告诉我我的代码有什么问题吗? 谢谢

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.methods import taxonomies

wp = Client('http://127.0.0.1/15wp/xmlrpc.php', 'admin', '123456')

# now let's create a new product
widget = WordPressPost()
widget.post_type = 'job_listing'
widget.title = 'Widgetlast02'
widget.content = 'This is the widgets description.'
widget.post_status = 'publish'
widget.custom_fields = []
widget.custom_fields.append({
        'job_location': 'Newyork',
        'job_listing_category':  'hotel'
})
widget.id = wp.call(posts.NewPost(widget))

【问题讨论】:

    标签: python-3.x wordpress xml-rpc


    【解决方案1】:

    这只是看documentation for WordPressPost in wordpress_xmlrpc的猜测:

    (强调我的)

    类 WordPressPost

    表示帖子、页面或其他已注册的自定义帖子类型 WordPress。

    • 身份证
    • 用户
    • 日期(日期时间)
    • date_modified(日期时间)
    • 蛞蝓
    • post_status
    • 标题
    • 内容
    • 摘录
    • 链接
    • comment_status
    • ping_status
    • 术语(WordPress术语列表)
    • terms_names(字典)
    • custom_fields (dict)
    • 附件(字典)
    • 密码
    • post_format
    • 缩略图
    • 粘性
    • post_type

    它期望custom_fieldsdict - 你正在创建一个list,然后将一个dict 插入到该列表中:

    widget.custom_fields = []
    widget.custom_fields.append({
            'job_location': 'Newyork',
            'job_listing_category':  'hotel'
    })
    

    这可能会更好:

    widget.custom_fields = {
            'job_location': 'Newyork',
            'job_listing_category':  'hotel'
    }
    

    【讨论】:

    【解决方案2】:

    custom_fields 属性需要一个字典列表。

    在每个 dict 中,它需要 keyvalue 字段的值。

    这里,key 是自定义字段的名称,value 是您要分配给它的值。

    以下是您的具体示例的 sn-p。

    widget.custom_fields = [
        {
            'key': 'job_location',
            'value':  'Newyork'
        }, 
        {
            'key': 'job_listing_category',
            'value':  'hotel'
        }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      相关资源
      最近更新 更多