How do I add meta when creating a post with rest api?

I have this code that inserts a post into WordPress site using Rest API:$ch = curl_init();curl_setopt($ch, CURLOPT_URL,

I have this code that inserts a post into WordPress site using Rest API:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $row->site_url."/wp-json/wp/v2/posts");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer '.$row->token
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "title"=>$row->title,
    "content"=>$row->content,   
    "excerpt"=>$row->excerpt,                                                                                                                               
    "status"=>'publish',
    "categories"=>(int)$row->c_wp_id
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$result_decoded = json_decode($result);
$post_id=$result_decoded->id;

I need to add meta , how do I do that?

I have tried this but it's not saving the meta:

$tst = (object)array(
    'some_key' => 'some_test_again'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $row->site_url."/wp-json/wp/v2/posts/".$row->wp_post_id);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer '.$row->token
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "title"=>$post_array['title'],
    "content"=>$post_array['content'],  
    "excerpt"=>$post_array['excerpt'],                                                                                                                          
    "meta"=>$tst
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$result_decoded = json_decode($result);
$post_id=$result_decoded->id;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745628889a4636980.html

相关推荐

  • How do I add meta when creating a post with rest api?

    I have this code that inserts a post into WordPress site using Rest API:$ch = curl_init();curl_setopt($ch, CURLOPT_URL,

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信