Publish a post using XML-RPC WordPress API and Python with category

2024/9/21 8:14:04

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

Answer

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内容返回抱歉,文章类型不支持您的分类法.错误

https://en.xdnf.cn/q/72078.html

Related Q&A

Django registration email not sending

Ive been trying to get the django-registration-redux account activation email to send to newly registered users.Ive gotten all non-email related parts to work, such as loggin in/out and actually regist…

NumPy data type comparison

I was playing with comparing data types of two different arrays to pick one that is suitable for combining the two. I was happy to discover that I could perform comparison operations, but in the proces…

A simple method for rotating images in reportlab

How can we easily rotate an image using reportlab? I have not found an easy method. The only way found comes from http://dods.ipsl.jussieu.fr/orchidee/SANORCHIDEE/TEMP/TEMP_LOCAL/cdat_portable/lib_new…

XML header getting removed after processing with elementtree

i have an xml file and i used Elementtree to add a new tag to the xml file.My xml file before processing is as follows <?xml version="1.0" encoding="utf-8"?><PackageInfo …

How to ntp server time down to millisecond precision using Python ntplib?

I am creating a python module that will output the time from a selection of NTP Pool servers to millisecond precision as an exercise in showing how server timestamps vary. Thus far I have been able to …

Control 2 separate Excel instances by COM independently... can it be done?

Ive got a legacy application which is implemented in a number of Excel workbooks. Its not something that I have the authority to re-implement, however another application that I do maintain does need t…

nested Python numpy arrays dimension confusion

Suppose I have a numpy array c constructed as follows:a = np.zeros((2,4)) b = np.zeros((2,8)) c = np.array([a,b])I would have expected c.shape to be (2,1) or (2,) but instead it is (2,2). Additionally,…

how to reuse tests written using unittest.testcase

Ive written some tests using unittest as below and I want to reuse them in another class where Im stuck and need help.. Code snippets are as below.MyTestClass.pyClass MyTestClass(unittest.TestCase): …

Randomized stratified k-fold cross-validation in scikit-learn?

Is there any built-in way to get scikit-learn to perform shuffled stratified k-fold cross-validation? This is one of the most common CV methods, and I am surprised I couldnt find a built-in method to …

Find all paths through a tree (nested dicts) from top to bottom

EDIT: See below for a suggested answer and how its not quite right yet.There are many similar questions to this one on Stack Overflow, but none exactly like it in Python. Im a programming novice, so pl…