【问题标题】:Wordpress XML-RPC post to a specific categoryWordpress XML-RPC 发布到特定类别
【发布时间】:2012-08-23 08:28:48
【问题描述】:

我已经尝试了很长时间,但它仍然只发布为“未分类”, 在文档中声明使用整数值作为类别 ID,但这不起作用。我还尝试按原样以小写形式编写类别名称,并输入 slug。根据文档,我做的一切都是正确的,但它仍然不起作用! wp.newPost,因为它使用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 );
}

【问题讨论】:

    标签: php wordpress xml-rpc


    【解决方案1】:

    嘿,我最近开始使用新的 API。

    您应该按照新文档中的说明在 XML-RPC 请求中使用 terms_names 参数:

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

    例子:

    您的代码应更改为如下所示。

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

    我对这个 API 方法有一个问题,但是 Post ID 不会只返回一个错误的布尔值。让我知道您是否有任何插入类别的运气以及您是否设法收到 POST ID。

    【讨论】:

    • 效果很好,完全符合我的要求。是的,我确实得到了一个 Post ID,因为它通过 XML 与 WP 通信,所以响应格式相同。 <?xml version="1.0"?> <methodResponse> <params> <param> <value> <string>3593</string> </value> </param> </params> </methodResponse>
    【解决方案2】:

    谢谢,这是救生员!如果您需要为您的帖子提供标签,您可以使用tags_input 属性在数组中传递它们,如下所示:

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

    如果需要传入具体的时间戳,应该使用如下格式

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

    并使用post_datepost_date_gmt 属性传递它:

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

    希望它对未来的人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多