使用XML-RPC WordPress API和Python发布带有类别的帖子 [英] Publish a post using XML-RPC WordPress API and Python with category

查看:120
本文介绍了使用XML-RPC WordPress API和Python发布带有类别的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个网站迁移到另一个使用Wordpress的网站。

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.

然后我用Python写下了一个脚本(改编自本文),它使用自3.4.x版本以来受支持的新Wordpress XML-RPC API从数据库获取帖子,并将其远程推送到新(测试)网站上。

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.

目前,我可以发布具有正确帖子类型的新帖子。但是如果指定类别,wordpress总是会向我返回此错误:

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: <Fault 401: 'Sorry, one of the given taxonomies is not supported by the post type.'>

我确定给定分类法支持帖子类型。我认为我使用了错误的语法来指定类别ID。代码如下:

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是与cu相关的分类法的名称stom类型为新闻。 term-id是类别的ID,我是使用phpMyAdmin发现的。

"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 :)

推荐答案

XML-RPC WordPress API文档说:

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.

这意味着term和terms_names是目录,键名是您想要的分类法名称,并且值是一个数组列表。

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]} 

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

,其中我的类别是您的分类法名称。

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

一些信息来自:解决Python发布wordpress内容返回抱歉,文章类型不支持您的分类法。错误

这篇关于使用XML-RPC WordPress API和Python发布带有类别的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆