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)

19 thoughts on “Using Python to add new posts in WordPress

  1. Permalink  ⋅ Reply

    Harshad Joshi

    June 12, 2010 at 7:31am

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

    How to post comments using XML RPC?

    • Permalink  ⋅ Reply


      June 12, 2010 at 7:49am

      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. Permalink  ⋅ Reply


    January 9, 2011 at 7:20pm

    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. Permalink  ⋅ Reply

    Garage Flooring

    June 24, 2011 at 8:54pm

    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. Permalink  ⋅ Reply


    November 27, 2012 at 6:49pm

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

  5. Permalink  ⋅ Reply


    February 14, 2013 at 12:00pm

    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!!!!

  6. Permalink  ⋅ Reply

    Tom Slee

    March 12, 2015 at 2:59am

    I know I’m many years late here, but I have been trying to use this module to post to my blog and any HTML markup in the content gets escaped (< becomes < etc). Do you have any idea how to avoid the escaping, and post HTML content?

    • Permalink  ⋅ Reply


      June 5, 2015 at 10:25pm

      Same issue here, you ever find a way around this?

  7. Permalink  ⋅ Reply


    April 13, 2016 at 4:14pm

    i’m sorry , can anybody help me ??, i want to know how can i delete a media from wordpress using XML-RPC

  8. Permalink  ⋅ Reply


    December 24, 2017 at 10:13pm

    how i can make it premlink like :

Leave a Reply

Your email will not be published. Name and Email fields are required.