Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress:如何以编程方式为每天添加帖子?_Php_Wordpress_Bulkinsert - Fatal编程技术网

Php Wordpress:如何以编程方式为每天添加帖子?

Php Wordpress:如何以编程方式为每天添加帖子?,php,wordpress,bulkinsert,Php,Wordpress,Bulkinsert,我想知道是否有可能有一个脚本,自动创建每日帖子“从今天”到“结束日期”(在脚本中手动设置)。因此,在每次迭代中,post_日期将是$date+1day 首先,我不知道这个脚本是否必须在functions.php或elsswhere中执行 第二,我是php新手,所以我知道如何用“wp_insert_post”创建一篇文章,但我不知道如何在循环中插入它 寻求帮助,如果有人有想法。。。 非常感谢是的,我们当然能做到。 从/wp includes/post.php开始,您可能需要遵循以下几个步骤: 将您

我想知道是否有可能有一个脚本,自动创建每日帖子“从今天”到“结束日期”(在脚本中手动设置)。因此,在每次迭代中,post_日期将是$date+1day

首先,我不知道这个脚本是否必须在functions.php或elsswhere中执行

第二,我是php新手,所以我知道如何用“wp_insert_post”创建一篇文章,但我不知道如何在循环中插入它

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

是的,我们当然能做到。
从/wp includes/post.php开始,您可能需要遵循以下几个步骤:

  • 将您的post_状态设置为future
  • 将发布日期设置为希望发布的时间
  • 按代码所示插入帖子

    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");
    
  • 每篇文章将在选定日期自动发布

  • 此功能由WordPress提供

  • 非常感谢,但是我可以在没有CRON的情况下使用它吗?只需将脚本例外一次,并从“今天”到“结束日期”创建尽可能多的帖子。请将此代码添加到当前激活主题的functions.php文件中,如果您遇到任何问题,请告诉我。