Wordpress XML-RPC post to a specific category

笑着哭i 提交于 2019-12-12 18:29:37

问题


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

回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/12087648/wordpress-xml-rpc-post-to-a-specific-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!