Adding Custom Fields to an Existing Post via XMLRPC in WordPress

If you’re having difficulty adding custom fields to an existing WordPress posts via XMLRPC’s metaWeblog.EditPost command, try including a dummy entry in your code.

It worked for me.

I’m working on a project where we’re programmatically adding WordPress custom field data to thousands of posts, seemed like a great job for XMLRPC. I had assumed a simple Ruby call like this would work:


result = server.call('metaWeblog.editPost', wordpress_post_id, name, pass, {"custom_fields" => [{"key" => "note", "value" => "loves you"]})

It doesn’t – it gives a less than happy Error 500 ‘Sorry, your entry could not be edited. Something wrong happened.’

I read through WordPress’s xmlrpc.php file and noticed that the update post command is run (line 2460) before any of the custom field data is recognized (line 2476).

So, I added a line to not change the Post’s title, and new custom fields were added as expected.


result = server.call('metaWeblog.editPost', wordpress_post_id, name, pass, {"title" => POST_TITLE, "custom_fields" => [{"key" => "note", "value" => "loves you"]})