我正在创建自己的插件,我创建了一个名为 $ titles_arr 的数组
它看起来像这样:
Array
(
[0] => Acrobotics Wants To Kickstart Smarter Cities With Its Smart Citizen Environment Sensors
[1] => Leaked Memo Shows Barnes & Noble Bringing Web Browser And Email To Simple Touch eReaders In June
[2] => How Cheap Genetic Testing Complicates Cancer Screening For Us All
[3] => Android’s Design Principles And The Calculus Of The Human Pleasure Response
[4] => Iterations: How Tech Hedge Funds And Investment Banks Make Sense Of Apple’s Share Buybacks
[5] => Yahoo Board Has Approved A $1.1 Billion Cash Deal For Tumblr, WSJ Reports
)
我需要将这些“标题”保存为帖子。我的意思是,将创建6个帖子,每个帖子都有一个来自数组的标题。
日期,正文,摘录等其他内容可能是默认值或null。如果需要,我稍后会在管理员中更改它们。
我想将状态设置为draft
而不是发布。
如何完成此类任务的最佳做法是什么?我正在创建自己的插件,需要一些建议如何在wordpress中一次保存多个帖子。
答案 0 :(得分:1)
你可以试试这个
global $user_ID; // logged in user id
$term = get_term_by('name', 'Php', 'category'); // Category name assumed 'Php'
$cat = $term->term_id; // get the id of Php category
$titles = array('Post-One', 'Post-Two'); // your titles array
foreach($titles as $title)
{
$new_post = array(
'post_title' => $title,
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'draft',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $userID,
'post_type' => 'post',
'post_category' => array($cat)
);
wp_insert_post($new_post); // insert the post
// Or
$newPostId = wp_insert_post($new_post); // the new post ID in $newPostId
}