Wordpress:如何以编程方式每天添加 post?

Wordpress: How to programmatically add post for each day?

我想知道是否有可能有一个脚本每天自动创建 posts "from today" 到 "end date"(在脚本中手动设置)。所以在每次迭代中 post_date 将是 $date +1day.

首先我不知道这个脚本是否必须在 functions.php 或其他地方执行...

其次,我是 php 的新手,所以我知道如何使用 "wp_insert_post" 创建 1 个单曲 post 但我不明白如何将其插入循环.

寻求帮助,如果有人有想法... 非常感谢

是的,我们当然可以。
通过 /wp-includes/post.php 的挖掘,您可能需要执行以下几个步骤:

  1. 将你的 post_status 设置为未来。
  2. 将 post_date 设置为您希望发布的时间。
  3. 如代码所示插入 post。

    function daily_post_article() {
    
            $begin = new DateTime("2018-11-01");
            $end = new DateTime("2018-12-15");
    
            $interval = DateInterval::createFromDateString("1 day");
            $period = new DatePeriod($begin, $interval, $end);
    
            foreach ($period as $dt) {
                $publishDate = $dt->format("Y-m-d");
                $postTitle = "Daily Post Title => ".$publishDate;
    
                if ( !get_page_by_title( $postTitle, "OBJECT", "post" ) ){
                    $args = array(
                        "post_title"=> "Daily Post Title => ".$publishDate, 
                        "post_type"=>"post", 
                        "post_date" => $publishDate,
                        "post_status"=>"future"
                    );        
                    $time = strtotime( $postdate . " GMT" );
                    $post_id = wp_insert_post( $args );
                    wp_schedule_single_event( $time, "publish_future_post", array( $post_id ) );
                }
            }
    }
    add_action("wp", "daily_post_article");
    
  4. 每个 post 将在选定的日期自动发布。

  5. 此功能由 WordPress 提供。