6

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

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

franzlorenzon
  • 5,845
  • 6
  • 36
  • 58

1 Answers1

10

XML-RPC WordPress 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 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.

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

franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
E Hong
  • 116
  • 2
  • 3
  • 1
    The documentation on this specific issue is horrible. Thanks for sharing! This is super useful! – jperelli Oct 18 '13 at 18:56
  • 1
    Hi, I been looking around for a way to set the taxonomy parent using wp.newpost and I can't find it (I already posted a question with no answer) so if someone could help I will deeply appreciate it. Thanks – JordanBelf Jul 19 '14 at 00:09
  • I cannot get this example working. I'm always getting the error: ''. Does anyone have any working examples for python? – disruptive Jul 30 '19 at 15:31
  • Here is a working example for python: `post.terms_names = {'post_tag': ['test', 'firstpost'],'category': ['Introductions', 'Tests']}` makes the post under 'Introductions' and 'Tests' categories – ZhouW Apr 03 '21 at 00:33