python wordpress xmlrpc 批量,使用XML-RPC WordPress API和Python发布带有类别的帖子

I'm doing a migration from a website to another one which use Wordpress.

I created new custom types for my needs (with the plugin Custom Post Types), and I created categories for each custom type.

I then wrote down a script in Python (adapted from this article), which gets the posts from the db and pushes them remotely on the new (testing) website, using the new Wordpress XML-RPC API supported since version 3.4.x.

At the moment I can publish a new post with the correct post type. But if I specify a category, wordpress always returns me this error:

xmlrpclib.Fault:

I'm sure that the post type is supported by the given taxonomy. I think I'm using a wrong syntax to specify the category id. Here's the code:

import datetime, xmlrpclib, MySQLdb

def post_remotely(post_data):

wp_url = "[my wordpress blog url]"

wp_username = "[myuser]"

wp_password = "[mypasswd]"

wp_blogid = "0"

status = 'publish'

server = xmlrpclib.ServerProxy(wp_url)

data = { 'post_title': post_data['title'], 'post_content': post_data['content'],

'post_date': post_data['data'], 'post_type': post_data['post_type'], 'terms': post_data['categories'],

'post_status': status }

post_id = server.wp.newPost(wp_blogid, wp_username, wp_password, data)

return post_id

And on the caller, to specify the category:

new_post['categories'] = [ { 'term_id': 3, 'taxonomy': 'news-cat' } ]

"news-cat" is the name of the taxonomy associated to the custom type "news". "term-id" is the id of the category, which I found out using phpMyAdmin.

I've also tried other approaches but to no avail. Without the category it works nicely.

Thanks in advance for any help :)

解决方案struct terms: Taxonomy names as keys, array of term IDs as values.

struct terms_names: Taxonomy names as keys, array of term names as values.

This means terms and terms_names are directory, the key name is the name of taxonomy you want, and the value is an array list.

If you want to set a category, you should set

‘terms‘:{‘my-category’:[4]}

or

‘terms_names’:{‘my-category’:["Wordpress"]}

in the post structure, where "my-category" is the name of your taxonomy.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值