是的,如果 WordPress 版本为 3.5 或更高版本,则可以这样做,当使用上传文件/图像的代码时,您可以设置 post_id。
我用于带有特色图片的新帖子的流程如下:
使用newPost函数发布没有特色的内容
image 并将publish设置为false,记录返回的post_id
这
上传图片并将post_id设置为刚刚发布的帖子的id
发布,记录image_id
完成后编辑帖子并将 wp_post_thumbnail 设置为等于
您刚刚上传的 image_id 并将发布设置为 true(如果需要)
重要的:
mime 类型很重要,它必须是“image/jpg”或“image/png”,请参阅文档,如果 mime 类型像“jpg”一样磨损,则附加将失败。
提示:
对于调试,如果您从 WordPress 收到一般错误,并且无法弄清楚为什么,您可以检查 WordPress 代码,甚至编辑它,添加调试/跟踪调用,希望您能找出原因。
这是带有类别、图像和标签的帖子示例。它需要 class-IXR.php
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-IXR.php
和 mime_content_type 函数
https://github.com/caiofior/storebaby/blob/master/magmi/plugins/extra/general/socialnotify/wp/mimetype.php
$client = new IXR_Client($url);
$content = array(
'post_status' => 'draft',
'post_type' => 'post',
'post_title' => 'Title',
'post_content' => 'Message',
// categories ids
'terms' => array('category' => array(3))
);
$params = array(0, $username, $password, $content);
$client->query('wp.newPost', $params);
$post_id = $client->getResponse();
$content = array(
'name' => basename('/var/www/sb/img.jpeg'),
'type' => mime_content_type('/var/www/sb/img.jpeg'),
'bits' => new IXR_Base64(file_get_contents('/var/www/sb/img.jpeg')),
true
);
$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content);
$media = $client->getResponse();
$content = array(
'post_status' => 'publish',
// Tags
'mt_keywords' => 'tag1, tag2, tag3',
'wp_post_thumbnail' => $media['id']
);
$client->query('metaWeblog.editPost', $post_id, $username, $password, $content, true);