WordPress, custom taxonomy, and XML-RPC

[Tweet : nvALT]

WordpresslogopeelingThis is a quick tip for anyone who may be searching for an answer to the question: “Is there any way to use XMLRPC to send custom post types to WordPress and attach custom taxonomy terms to them?” Yes, there is. It took me longer than it should have to find it, but I’ve successfully uploaded images, and attached them to a “howtos” post type with “level” and “topic” set under its custom taxonomies.

The custom post part is easy, and if you’re working with XML-RPC, you’ve probably already figured it out. Just set your ‘post_type’ to your registered custom post type when you pass the construct to create a new post.

Then it got sticky for me. I ended up finding a nearly 2-year-old patch to the xmlrpc.php file with the answer, and I was pleasantly surprised to see it had already been applied in current versions. So much for documentation. The construct just needs an element called mt_taxonomy, and it takes an associative array with two keys: tags and taxonomy. tags holds the terms you want to apply, either as another array or in a comma-separated list, and taxonomy is the name of the custom taxonomy they’re assigned/available to.

I ended up adding my own methods to the MetaWeblog interface for handling my specific post types. Quick example:

add_filter('xmlrpc_methods', 'bt_xmlrpc_methods');
function bt_xmlrpc_methods($methods)
{
	$methods['metaWeblog.addHowTo'] = 'bt_add_howto';
	return $methods;
}

Then a function called bt_add_howto receives the $args, which will generally be:

$post_ID         = (int) $args[0];
$username        = $args[1];
$password        = $args[2];
$content_struct = $args[3];
$publish         = $args[4];

At this point, I think I’m just taking notes for myself. Hopefully this pointer will be enough to help someone else not spend an hour banging their head against the desk. Custom post types are fun, and growing more flexible, but there are a lot of walls you can run into right now.