簡體   English   中英

使用wordpress API將自定義字段添加到新的wordpress帖子

[英]Add custom fields to a new wordpress post using the wordpress API

我需要以編程方式創建新的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;
}

經過一些實驗,結果證明$ custom_fields需要像這樣指定:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM