February 1st, 2011
How to pass post categories to WordPress via XML-RPC & CodeIgniter
That’s a mouthful of a title! I have been trying to create new posts on a WordPress blog via XML-RPC and CodeIgniter, but couldn’t get the categories applied properly. Here’s how I got it working (no guarantees this will work in every situation, or that it will work in an hour).
I am using CodeIgniter’s XMLRPC library and the metaWeblog API. To get categories inserted with a post, you first need to know the names of the categories — you can do this via metaWeblog’s getCategories method — in this case, I hard-coded the values into my request for easier testing.
Put the category names into an array, and pass the categories array into the post data, appending ‘struct’ after the array (the ‘struct’ is the important part).
$categories = array("Uncategorized", "Blogroll", "Reviews");
$blogpass = $blogpass;
$blogid = 1;
$publishImmediately = TRUE;
$thePost = array(
array(
'title' => array($post['title'],'string'),
'description' => array($post['text'],'string'),
'categories' => array($categories,'struct'),
'post_type' => array('post','string'),
), 'struct');
$this->xmlrpc->server($blogurl, 80);
$this->xmlrpc->method('metaWeblog.newPost');
$request = array($blogid, $bloguser, $blogpass, $thePost, $publishImmediately);
$this->xmlrpc->request($request);
$result = $this->xmlrpc->send_request();