我正在使用wp_insert_post()
功能创建包含自定义数据的帖子,但我无法在帖子中添加类别和精选图片
我的代码如下:
$new_post = array(
'post_title' => $leadTitle,
'post_content' => $leadContent,
'post_status' => $postStatus,
'post_date' => $timeStamp,
'post_author' => $userID,
'post_type' => $postType,
'post_category' => array('language'),
'tags_input' => array('Language',"Temp")
);
$post_id = wp_insert_post($new_post);
有什么不对吗?
答案 0 :(得分:1)
wp_insert_post()
的post_category
参数应作为类别ID的数组提供,如果您的类别具有层次结构(对于默认的帖子类型,它是这样的):
'post_category' => array(90, 100)
另外,请确保数组中的ID提供为int
。
您可以在documentation。
中看到更详细的信息如果您在网站的Media Library
中添加了图片,则可以将其用作帖子/页面的附件,使用它ID
,如:
'_thumbnail_id' => 110
如果您没有,请先使用wp_insert_attachment()
功能将图片添加为attachment
类型。
所以,你的功能看起来会是:
$new_post = array(
'post_title' => $leadTitle,
'post_content' => $leadContent,
'post_status' => $postStatus,
'post_date' => $timeStamp,
'post_author' => $userID,
'post_type' => $postType,
'post_category' => array(99),
'tags_input' => array('Language',"Temp"),
'_thumbnail_id' => 110
);
$post_id = wp_insert_post($new_post);
答案 1 :(得分:0)
Please check below code:
$my_post = array();
$my_post['post_title'] = 'My post';
$my_post['post_content'] = 'This is my post.';
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array(0);
// Insert the post into the database
wp_insert_post( $my_post );"
或者
$new_post = array(
'post_title' => $leadTitle,
'post_content' => $leadContent,
'post_status' => $postStatus,
'post_date' => $timeStamp,
'post_author' => $userID,
'post_type' => $postType,
'post_category' => array('language'),
'tags_input' => array('Language',"Temp")
);
$post_id = wp_insert_post($new_post);