Forums

Add Custom Fields To A Post Using XML-RPC (7 posts)

  1. theimben
    Member
    Posted 2 weeks ago #

    I'm using the XML-RPC to add posts and I want to add custom fields to it the post.

    How do I do this?

    Here is the function I'm currently using

    function API_newPost($title,$body,$rpcurl,$username,$password,$categories=array(1)){
    	$categories = implode(",", $categories);
    	$XML = "$title".
    	"$categories".
    	$body;
    	$params = array('','',$username,$password,$XML,1);
    	$request = xmlrpc_encode_request('blogger.newPost',$params);
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    	curl_setopt($ch, CURLOPT_URL, $rpcurl);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    	curl_exec($ch);
    	curl_close($ch);
    }
  2. josephscott
    Member
    Posted 2 weeks ago #

    The blogger.newPost method doesn't support custom fields, but the metaWeblog.newPost method does.

  3. theimben
    Member
    Posted 2 weeks ago #

    I can't work out how to do this. This is what I've tried but it doesn't work. It adds the post but not the custom fields.

    function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$categories=array(1)){
    	$categories = implode(",", $categories);
    	$XML = "<title>$title</title>".
    	"<category>$categories</category>".
    	$body;
    	$cflds = array('name' => 'a name', 'url' => 'http://www.google.com');
    	$content = array('post_type' => 'post', 'title' => $title, 'description' => $body, 'custom_fields' => $cflds);
    	$params = array('',$username,$password,$content,$XML,1);
    	$request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    	curl_setopt($ch, CURLOPT_URL, $rpcurl);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    	curl_exec($ch);
    	curl_close($ch);
    	echo '<pre>'.$request;
    }
  4. josephscott
    Member
    Posted 1 week ago #

    Custom fields are a set of key/value pairs that look like:

    "custom_fields" = (
            {key = city; value = Sacramento; },
            {key = city; value = Sandy; }
        )

    Try fetching post data with metaWeblog.getPost for a post that already has custom fields and you'll see what they look like.

  5. theimben
    Member
    Posted 1 week ago #

    That makes more sense but how would I put that in the function I have?

  6. josephscott
    Member
    Posted 1 week ago #

    array(
      array( 'key' => 'city', 'value' => 'Sacramento' ),
      array( 'key' => 'city', 'value' => 'Sandy' )
    )
  7. theimben
    Member
    Posted 1 week ago #

    Thank you :)

Reply

You must log in to post.

About this Topic