简体   繁体   English

如何一次保存/更新Wordpress中的帖子数组?

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

I am creating my own plugin and I have created an array called $titles_arr 我正在创建自己的插件,并创建了一个名为$ titles_arr的数组

and it looks like this: 它看起来像这样:

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
)  

and I need to save this "titles" as posts. 而且我需要将此“标题”另存为帖子。 I mean, 6 posts will be created and each of them will have the one title from the array. 我的意思是,将创建6个帖子,每个帖子将从数组中获得一个标题。

Other things like date, body, excerpt etc. could be default or null. 日期,正文,摘录等其他内容可能是默认值或为null。 I will change them later in admin if needed. 如有需要,我将在以后的管理员中更改它们。

And I would like to set state to draft instead of published. 我想将状态设置为draft而不是发布。

What is the best practice how to do such task? 什么是最佳做法,怎么做? I am creating my own plugin and need some advice how to save multiple posts at once in wordpress. 我正在创建自己的插件,并且需要一些建议,如何在Wordpress中一次保存多个帖子。

You may try this 你可以试试这个

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM