WordPress XMLRPC (wp.newPost) – Can’t get post_date formatted right

    $content = array(
        'post_title' => $title,
        'post_content' => $body,
        'post_status' => 'publish', 
        'post_date' => $pub_date //What's the proper format for the date here?
    )

    $params = array(0,$this->settings['username'],$this->settings['password'],$content);
    $request = xmlrpc_encode_request('wp.newPost',$params);
    $this->Curl->post($this->controller->rpc_url,$request); 

I’ve tried many different variations for post_date format and none of them worked. Here are all the combinations that I’ve already tried and none of them are working:

1) $pub_date = date('Y-m-d H:i:s', time());
2) $pub_date = time();
3) $pub_date = new IXR_Date(time());
4) $pub_date = date('c',time());
5) $datetime = new DateTime('2010-12-30 23:21:46');
   $pub_date = $datetime->format(DateTime::ISO8601);

It seems like I tested every possible solution and it still doesn’t want to post whenever I try to include post_date. Could someone please help, I’m really stuck on this one.

Related posts

Leave a Reply

2 comments

  1. Figured it out:

      $publish_date = '20121217T01:47:03Z' //this is the proper format for datetime 
      xmlrpc_set_type($publish_date, 'datetime'); //xmlrpc_set_type must be used on above date so that XML passes it properly as <dateTime.iso8601> instead of <string>
    
        $content = array(
            'post_title' => $title,
            'post_content' => $body,
            'post_status' => 'publish', 
            'post_date' => $publish_date);
    
        $params = array(0,$this->settings['username'],$this->settings['password'],$content);
        $request = xmlrpc_encode_request('wp.newPost',$params);
        $this->Curl->post($this->controller->rpc_url,$request); 
    
  2. You can use code available on WordPresse Core:

    require '/ROOT/maaal/wp-includes/class-IXR.php';
    
    $timestamp = time(); // Or some value you calculated somewhere
    $xmlrpc_date = new IXR_Date($timestamp);
    

    PS: $time can be a PHP timestamp or an ISO one.