首页 > 解决方案 > 如何通过 python wordpress xmlrpc 提交自定义字段数据

问题描述

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

截图 截图

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

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.xwordpressxml-rpc

解决方案


这只是查看in文档WordPressPostwordpress_xmlrpc的猜测:

(强调我的)

类 WordPressPost

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

  • ID
  • 用户
  • 日期(日期时间)
  • date_modified(日期时间)
  • 蛞蝓
  • post_status
  • 标题
  • 内容
  • 摘抄
  • 关联
  • 评论状态
  • ping_status
  • 条款(WordPress 条款列表)
  • 术语名称(字典)
  • custom_fields (字典)
  • 外壳(字典)
  • 密码
  • post_format
  • 缩略图
  • post_type

它期望custom_fieldsdict- 您正在创建 alist然后将 a 插入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'
}

推荐阅读