wp_remote_post创建帖子两次

时间:2019-03-20 09:51:09

标签: wordpress rest api

我已将此代码放在single.php中只是为了进行测试。页面加载后,在草稿中创建了两个帖子。但是在$ api_response中,我只看到创建了一个帖子。问题是什么? (使用“应用程序密码”插件授权)

$api_response = wp_remote_post( 'https://example.com/wp-json/wp/v2/posts', array(
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( 'testAdmin:DG4D 0KWq 81VO gSmB fTsM p2LF' )
    ),
    'body' => array(
        'title'   => 'Lorem ipsum dolor sit amet',
        'status'  => 'draft', // publish/draft
        'content' => 'Aenean nec ipsum at augue scelerisque finibus. Phasellus nec lorem iaculis, suscipit velit sed, blandit elit.',
//      'categories' => 5, // category ID
//      'tags' => '1,4,23', // string, comma separated
//      'date' => '2015-05-05T10:00:00', // YYYY-MM-DDTHH:MM:SS
        'excerpt' => 'Aenean nec ipsum at augue scelerisque finibus.',
//      'password' => '12$45',
        'slug' => 'lorem-ipsum', // part of the URL usually
        // more body params are here:
        // developer.wordpress.org/rest-api/reference/posts/#create-a-post
    )
) );
$body = json_decode( $api_response['body'] );

if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
    echo 'The post "' . $body->title->rendered . '" has been created successfully';
}

echo "<pre>";
print_r( $body );
echo "</pre>";

0 个答案:

没有答案