【问题标题】:Add custom fields to a new wordpress post using the wordpress API使用 wordpress API 将自定义字段添加到新的 wordpress 帖子
【发布时间】:2013-08-08 21:00:22
【问题描述】:

我需要以编程方式创建新的 Wordpress 帖子并为每个帖子分配自定义字段。使用 Wordpress xmlrpc API,我可以成功添加新帖子,但自定义字段没有添加。

这是代码的摘录:

$blogid = 0;
$username = 'user';
$password = 'xyzzy1234';
$method = 'wp.newPost';
$title = "Post Title";
$pcontent = "I'm the post content.";
$categories = array('Cat 1', 'Cat 2');
$post_status = 'publish';  
$custom_fields = array('cccId' => '12345', 'cccType' => 'news');
$content = array(
                'post_type' => 'post',
                'post_status' => $post_status,
                'post_title' => $title,
                'post_content' => $pcontent,
                'terms_names' => array('category'=>$categories),
                'comment_status' => $comment_status,
                'ping_status' => $ping_status,
                'custom_fields' => $custom_fields
            );

$parameters = array($blogid, $username, $password, $content);
$response = sendRequest($method, $parameters);

function sendRequest($methodName, $parameters)  {
$request = xmlrpc_encode_request($methodName, $parameters);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, RPC_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
$results = print_r(xmlrpc_decode($results));
curl_close($ch);
return $results;
}

【问题讨论】:

    标签: php wordpress api


    【解决方案1】:

    经过一些实验,发现 $custom_fields 需要这样指定:

    $custom_fields = array(
                       array('key' => 'cccId', 'value' => '12345'),
                       array('key' => 'cccType', 'value' => 'news')
                     );
    

    【讨论】:

    • 谢谢!找不到任何准确的文档,直到我找到这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多