Wordpress XML-RPC post to a specific category

2019-07-20 01:25发布

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 );
}

2条回答
对你真心纯属浪费
2楼-- · 2019-07-20 02:13

Thanks, this is a life saver! If you need to provide tags for your post, you can pass them in array using tags_input property, like this:

$content['tags_input'] = "action, adventure, thriller";

If you need to pass in a specific time stamp, you should use the following format

D, d M Y H:i:s +0000

and pass it using the post_date or post_date_gmt property:

$content['post_date_gmt | post_date'] = date("D, d M Y H:i:s +0000", strtotime($your_specific_date_and_time));

Hope it helps someone in the future!

查看更多
混吃等死
3楼-- · 2019-07-20 02:16

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.

查看更多
登录 后发表回答