Posting using XML-RPC WordPress API and Python with category

I am making the transition from a website to another that uses Wordpress.

I created new custom types for my needs (using custom plugin message types), and I created categories for each custom type.

Then I wrote a script in Python (adapted from in this article ) that receives messages from db and removes them remotely on a new (testing) website using the new Wordpress XML-RPC API supported from version 3.4.x.

At the moment, I can post a new message with the correct message 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.'>

I am sure that the message type is supported by this taxonomy. I think I am using the wrong syntax to indicate the category identifier. Here is 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 indicate the category:

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

"news-cat" is the name of a taxonomy associated with a custom type of "news". "term-id" is the category identifier that I recognized with phpMyAdmin.

I also tried other approaches, but to no avail. Without a category, it works beautifully.

Thanks in advance for any help :)

+5
source share
1 answer

WordPress XML-RPC API Document says:

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 that terms and terms_names are a directory, the key name is the name of the taxonomy you want, and the value is a list of arrays.

,

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

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

, " " - .

: 解决 Python 发布 wordpress 内容 返回 抱歉, 文章 类型 不 支持 您 的 分类 法. 错误

+9

All Articles