0

We are working on a platform that gets news from various sources and runs some NLP analysis on it.

We have the backbone of the backend down, we're now trying to output our results on a feed. Since articles are gathered using our automated engine, WP posts containing the articles and NLP analysis will be generated programmatically, not manually by users. We picked Wordpress as our CMS.

So far, I've had success with creating posts using this library (https://pypi.org/project/python-wordpress-xmlrpc/), which I could use to post data to Wordpress at the end of my data pipeline.

client = Client(url, username, password)
post = WordPressPost()
post.title = 'Elon Musk discovers a new element'
post.content = 'Lets look at how Elon Musk is actually Tony Stark.'
post.terms_names = {
  'post_tag': ['AI', 'musk'],
  'category': ['Technology', 'Chemistry']

post.post_status = 'publish'
post.id = client.call(posts.NewPost(post))

I read however that XML-RPC was outdated, and may lead to issues down the line. In my research, I found this also (https://pypi.org/project/wordpress-api/), but it's much less documented.

What are some options here? What else can I use to post data from Python to WP databases.

  • 1
    I would keep in mind that using a WP library in python will probably make your life harder, not easier. A generic REST APII library would be more useful. If it's a software recommendation you're looking for though you'd need to post on the software recommendation stack – Tom J Nowell Sep 14 at 10:40
1

Use the REST API at /wp-json, there are endpoints for retrieving posts that you can POST to.

You will need a plugin installed to provide authentication, I recommend OAuth2.

Then, you can use an OAuth2 and a REST API library in python. You do not need to use a WordPress specific library.

| improve this answer | |
  • Thanks for the reply. Not much concrete documentation on this out there. Have you any experience with the XMLRPC capabilities of Wordpress? When is it okay to use this line to pass data? – Pete Sep 14 at 11:09
  • It also seems that using the REST API requires for associative relationship to be created manually, i.e. when creating a post, categories can only be passed integers, thus the entry with the relevant category must first be created in the wp_terms table. It must then be returned or looked up, and added to the json entry of the data meant to go in the post. Same for meta-data all over again. It seems XML RPC handles this operations by itself – Pete Sep 14 at 12:01
  • I would not use XMLRPC, it's disabled by a lot of hosts, is much harder to use, has less scope for extension than the REST API, would not recommend. You'll need to ask additional questions on the site if you have additional questions though. Note that you won't see any Python code on this stack, we can only answer from the context of the WordPress end of things, so I can't share a Python example, only generics. You'll need to ask in Python communities how to do that part – Tom J Nowell Sep 14 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.