[wp-xmlrpc] Updating Custom Fields Using metaWeblog.editPost

Joseph Scott joseph at josephscott.org
Tue Apr 7 17:34:30 GMT 2009


On Apr 7, 2009, at 10:19 AM, Burt Adsit wrote:

> It looks to me like the only way to update the custom fields in a  
> post via this api is to get the post, remember the custom field ids  
> and then call edit post, referring to those custom field ids again  
> with updated info. Those custom field ids are unique to each post.



Correct.  When updating a custom field (in any way) you must provide  
the custom field id.  Obviously when creating custom fields as part of  
a metaWeblog.newPost call you just provide the key/value pair since  
the id is not known at that point.  This implies that you should turn  
around and get the post info for the post that you just created so  
that you'll have those ids should you want to update the custom fields  
later on.

Daniel was also correct in pointing out that custom field ids are  
unique across all blog posts, so that custom fields across different  
posts with the same key aren't associated with each other.

Since this thread has grown beyond David's original email to me about  
how to do this, here's a PHP snippet that calls metaWeblog.newPost  
with custom fields:

---------------------

$post = array(
     "title"         => "Test Me Please",
     "description"   => "Hi there!",
     "custom_fields" => array(
         array( "key" => "state", "value" => "Utah" ),
         array( "key" => "country", "value" => "USA" )
     )
);


$rpc = new IXR_Client( 'http://localhost/wp/trunk/xmlrpc.php' );
$status = $rpc->query(
     "metaWeblog.newPost",
     1,
     $username,
     $password,
     $post,
     false
);

---------------------


Let's assume that the new post_id is 4 and that we've called  
metaWeblog.getPost and determined that the custom field id for state  
on that post is 3.  A call to metaWeblog.editPost would look like:


---------------------

$post_id = 4;

$post = array(
     "title"         => "Test Me Please",
     "description"   => "Hi there!",
     "custom_fields" => array(
         array( "id" => 3, "key" => "state", "value" => "California" ),
     )
);


$rpc = new IXR_Client( $rpc_url );
$status = $rpc->query(
     'metaWeblog.editPost',
     $post_id,
     $username,
     $password,
     $post,
     false
);

---------------------



Just remember that every custom field in WP gets a unique id.


--
Joseph Scott
joseph at josephscott.org
http://josephscott.org/






More information about the wp-xmlrpc mailing list