Using Python to add new posts in WordPress

Wordpress is a web publishing platform, which allows you to add and edit content to your website. This can be done by visiting the administration interface, wp-admin. However, there is also an XML RPC programming interface that allows you to do the same from any programming language that supports this interface.

In this short article I will show how to add new posts to WordPress from Python. There are several libraries available that allow you to view and edit content, but they were all inappropriate for me. I wanted to be able to convert my old website into WordPress posts and have the published date set to the date I originally posted items on my old site. However, every library I tried either didn’t have the possibility to set the published date, or WordPress didn’t understand it.

Here’s the short piece of code that allowed me to add new posts to WordPress, without the use of any 3rd party library:

import datetime, xmlrpclib

wp_url = ""
wp_username = "someuser"
wp_password = "secret"
wp_blogid = ""

status_draft = 0
status_published = 1

server = xmlrpclib.ServerProxy(wp_url)

title = "Title with spaces"
content = "Body with lots of content"
date_created = xmlrpclib.DateTime(datetime.datetime.strptime("2009-10-20 21:08", "%Y-%m-%d %H:%M"))
categories = ["somecategory"]
tags = ["sometag", "othertag"]
data = {'title': title, 'description': content, 'dateCreated': date_created, 'categories': categories, 'mt_keywords': tags}

post_id = server.metaWeblog.newPost(wp_blogid, wp_username, wp_password, data, status_published)
This entry was posted in programming and tagged , . Bookmark the permalink.

10 Responses to Using Python to add new posts in WordPress

  1. Is there any python script for posting or receiving comments on a blog?

    How to post comments using XML RPC?

    • jansipke says:

      I haven’t looked into that. I would imagine it being used as a spam tool, so perhaps it isn’t even possible by default in WordPress.

  2. Alvin says:

    Thank you for posting this – was having bunches of trouble getting a custom date_created to be accepted

    took your code and used it for date_created_gmt and got it working

    thank you again

  3. Thanks so much for this! It’s really useful because I’ve been trying to run scripts to schedule posts automatically.

    It looks like you can’t create new categories on the fly just by supplying the array with new items. If you want to know what categories are already available for you to use, you can use this code (building off of the other code):

    categories = server.metaWeblog.getCategories(wp_blogid, wp_username, wp_password)

    for item in categories:
    print item[‘description’]

    (Remember to indent the print statement.)

  4. Pingback: How can I autopost on the wordpress with python? | Thinking in python

  5. Sigfried says:

    Extremely helpful! Thanks!!!

  6. Pingback: How can I autopost on the wordpress with python? | I love Python | Mostly on Python Programming

  7. Pingback: Publishing to WordPress From Mercurial DVCS with XML-RPC - Mike Levin

  8. kastechno says:

    Hello thank you for your post…How about updating an existing post?
    I need code

  9. rene says:

    Thank you so much for that great article. I were looking for a solution to generate automatic postings on a testing blog. I just tried your solution and it worked!!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s