Support » Fixing WordPress » Add Custom Fields To A Post Using XML-RPC

  • 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);
    }
Viewing 12 replies - 1 through 12 (of 12 total)
  • Joseph Scott

    (@josephscott)

    Member

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

    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;
    }
    Joseph Scott

    (@josephscott)

    Member

    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.

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

    Joseph Scott

    (@josephscott)

    Member

    array(
      array( 'key' => 'city', 'value' => 'Sacramento' ),
      array( 'key' => 'city', 'value' => 'Sandy' )
    )

    Thank you 🙂

    THANK YOU!

    Hi, I’m using this code to post using xmlrcp but it doesn’t seem to be parsing the cusom_fields.

    Any help would be much appreciated, thanks!

    <?php
    	include_once( 'wp-includes/class-IXR.php');
    
    	$method_name = "wp.newPage";
    
    	$rpc_url = "http://www.wheeloftimerp.net/xmlrpc.php";
    	$blog_id    = 1;
    	$username   = $_POST['reply_author'];
    	$password   = $_POST['reply_password'];
    	$publish    = true;    
    
     	$custom_fields = array(
     		"key"   => "Character",
     		"value" => $_POST['reply_character']
    		);
    
    	$post = array(
    	     "title"         => $_POST['reply_title'],
    	     "description"   => $_POST['reply_content'],
    		 "wp_page_parent_id" =>  $_POST['reply_parent'],
    		"custom_fields" => $custom_fields
    	);
    
    	$rpc = new IXR_Client( $rpc_url );
    	$status = $rpc->query(
    	     $method_name,
    	     $blog_id,
    	     $username,
    	     $password,
    	     $post,
    	     $publish
    	);
    
    	if( !$status ) {
    	     print "Error in RPC request\n";
    	     print_r( $rpc );
    	     exit;
    	}
    
    	print_r( $rpc->getResponse( ) );
    	print "\n";
    
    	?>

    Joseph Scott

    (@josephscott)

    Member

    The custom fields setup should look like this:

    "custom_fields" => array(
            array( "key" => "state", "value" => "Utah" ),

    has anyone been able to successfully call the NewPost method with Custom Fields? I am still having no luck.

    I am using a .Net Wrapper. I tried first as you all have suggested, with just key and value pairs. However, the Metablog Wrapper gives me an error that custom field ID cannot be null.

    So I tried giving it a random number as ID like so (code is in C# .Net). Now the Post gets inserted, but the custom fields are getting ignored ?!

    CustomField[] cf = new CustomField[1];
                    cf[0].id = "354325";  //random ID number
                    cf[0].key = "image_url";
                    cf[0].value = "http://somesite.com/someimage.jpg";
    Joseph Scott

    (@josephscott)

    Member

    See my example above, if all you are doing is inserting a new custom field entry you just need ‘key’ and ‘value’ since there is no ‘id’ yet. The ‘id’ field is only used when updating an existing value in some way.

    graytime

    (@zxy198717)

    I am success to call the NewPost method with Custom Fields,the demo below:

    function post($result){
            include("xmlrpc.inc");
        	$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
        	$c = new xmlrpc_client("xmlrpc.php","example.com", 80);
        	$content['title']="title";
        	$content['description']="content";
        	$content['mt_keywords']="tag1,tag2";
        	$content['wp_password']="";
        	$content['categories'] = array("label");
        	$content['custom_fields'] = array(
        	array( 'key' => 'city', 'value' => 'Sacramento' )
        	);
        	$x = new xmlrpcmsg("metaWeblog.newPost",
        	array(php_xmlrpc_encode("1"),
        	php_xmlrpc_encode("admin"),
        	php_xmlrpc_encode('841321'),
        	php_xmlrpc_encode($content),
        	php_xmlrpc_encode("1")));
    
        	$c->return_type = 'phpvals';
        	$r =$c->send($x);
        	if ($r->errno=="0")
        	echo "success,ID:".$r->val;
        	else {
        		echo "fail";;
        		print_r($r);
        	}
        }

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Add Custom Fields To A Post Using XML-RPC’ is closed to new replies.