2013-05-19 48 views
0

我創造我自己的插件,我已經創建了一個名爲數組$ titles_arr如何一次保存/更新Wordpress中的帖子數組?

,它看起來像這樣:

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中一次保存多個帖子。

回答

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 
} 
相關問題