Wordpress XML-RPC and featured images

2019-02-20 02:05发布

问题:

I'm currently developing a plugin for a client that takes an xml feed hourly and posts it into wordpress and I'm having trouble sending the featured image to the post.

I can post to wordpress fine but all my attempts to post the featured image have failed.

<?php
class XMLRPClientWordPress
{
var $XMLRPCURL = "";
var $UserName  = "";
var $PassWord = "";
// Constructor
public function __construct($xmlrpcurl, $username, $password)
{
$this->XMLRPCURL = $xmlrpcurl;
$this->UserName  = $username;
$this->PassWord = $password;
}
function send_request($requestname, $params)
{
$request = xmlrpc_encode_request($requestname, $params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $this->XMLRPCURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
return $results;
}
public function create_post( $title, $body )
{
$title = htmlentities( $title, ENT_NOQUOTES, 'UTF-8' );
$content = array(
    'post_category' => array( 18 ), // my category id
    'post_type' => 'post',
    'post_title' => $title,
    'post_content' => $body,
    'featured_image_url' => 'http://www.geekologie.com/2009/02/18/scary%20clown.jpg',
);

$params = array( 0, $this->UserName, $this->PassWord, $content );
return $this->send_request( 'wp.newPost', $params );
}
}
$objXMLRPClientWordPress = new XMLRPClientWordPress("xxxx/xmlrpc.php" , "xxxxx" , "xxxx");
$objXMLRPClientWordPress->create_post('Hey Chloe','Hope you like the clown'); 
?>

Is what i currently have, i've been reading the wordpress patch tickets on this issue but can't seem to figure out how to actually use the new features

Thanks

回答1:

It should be something like this

   'wp_post_thumbnail' => $pictureid

You have to get the ID of uploaded picture into variable and then assign it as a value of 'wp_post_thumbnail' parameter for 'metaWeblog.newPost'