Using WordPress XML-RPC wp.uploadFile and wp.newPost from PHP

I’m using WordPress XML-RPC to automatically post to my blog and I got these two functions working from PHP: wp.newPost and wp.uploadFile.

However, when I run them in one php script as shown below: (just included the important parts) WordPress is not detecting attachment_id from wp.uploadFile when I try to post even though attachment_id exists.

Read More
//wp.newPost content
$content = array(
'post_title' => $title,
'post_content' => $body,
'post_status' => 'publish', 
'post_type' => 'products',
'post_thumbnail' = // wp.uploadFile is called here that returns attachement id
);

$this->Curl->post($url,$content);

When I try to run above code I get: ” faultCode 404 faultString Invalid attachment ID. “

I verified that the wp.uploadFile image has been uploaded successfully and its showing in the WordPress library. In fact, if I run the script again and just replace the ‘post_thumbnail’ value with the same exact attachement_id that was return by wp.uploadFile, it works!

So, apparently WordPress is not detecting that the image has been uploaded if I run the two functions in conjunction as shown above. Is there any solution to this? I would really hate to store WordPress attachement id’s in my own database.

Related posts

Leave a Reply

1 comment

  1. to post via xmlrpc i’m using IXR

    require_once("IXR_Library.php.inc");
    

    the below is what i’m using; it will need certainly some edits but might give you some clue

    to upload a new file:

    $file_upload_path = get_post_meta ( $image->ID,'_wp_attached_file', false);
    $file_path = $_SERVER['DOCUMENT_ROOT']."/dev/0.2/wp-content/uploads/".$file_upload_path[0];
    $path_parts = pathinfo($file_path);
    $file_name = $path_parts['filename'].'.'.$path_parts['extension'];
    $data = array(
      'name'  => $file_name ,
      'type'  => $image->post_mime_type ,
      'bits'  => new IXR_Base64( file_get_contents( $file_path ) ) ,
      'overwrite' => true
    );
    $status = $rpc->query(
        'wp.uploadFile', 
        '1',
        $username,
        $password,
        $data
    );
    $image_returnInfo = $rpc->getResponse();
    

    to create a new post:

    $data = array(
        'post_title' => $post->post_title,
        'post_content' => $post->post_content . $attachement_string_hack,
        'post_type' => 'product',
        'post_status' => 'publish',
        'dateCreated' => (new IXR_Date(time())),
        'post_thumbnail' => $image_returnInfo['id']
    );
    $rpc = new IXR_Client( $rpc_url );
    $res = $rpc->query('wp.newPost', '', $username, $password, $data, 0);
    

    hope this helps!
    (i’m now stuck with attaching images to post)