Search code examples
phpwordpressxml-rpc

Wordpress XML-RPC post to a specific category


I've been trying for quite a long time and still it posts only as "Uncategorized", In the documentation it's stated to use integer value as category ID, but that doesn't work. I've also tried writing category name as it is and in lowercase and also entering slug. According to documentation I'm doing everything right, but it still doesn't work! wp.newPost and since it uses wp_insert_post().

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_status' => 'pending',
        'post_title' => $title,
        'post_content' => $body,
        'comment_status' => 'closed',
    );

    $params = array( 0, $this->username, $this->password, $content );
    return $this->send_request( 'wp.newPost', $params );
}

Solution

  • Hey I've recently started using the new API.

    You should use the terms_names parameter in your XML-RPC request as stated in the new docs:

    http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost

    Example:

    Your code should be changed to look something like this.

    public function create_post( $title, $body )
    {
        $title = htmlentities( $title, ENT_NOQUOTES, 'UTF-8' );
        $content = array(
            'post_type' => 'post',
            'post_status' => 'pending',
            'post_title' => $title,
            'post_content' => $body,
            'terms' => array('category' => array( 18 ) ),
            'comment_status' => 'closed',
        );
    
        $params = array( 0, $this->username, $this->password, $content );
        return $this->send_request( 'wp.newPost', $params );
    }
    

    I have one problem with this API method however, the Post ID is not returned only a false boolean value. Let me know if you have any luck with inserting categories and if you manage to receive the POST ID.