如何保存/更新数组的文章在Wordpress一次


How to save/update array of posts in Wordpress at once?

我正在创建自己的插件,我已经创建了一个名为$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个帖子将被创建,每个将有一个标题从数组。

其他内容,如日期,正文,摘录等可以是默认的或空的。如果需要,我会稍后在admin中更改它们。

我想将state设置为draft而不是published。

如何完成这样的任务的最佳实践是什么?我正在创建自己的插件,需要一些建议如何保存多个帖子在wordpress一次。

你可以试试这个

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
}