简体   繁体   English

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

[英]Wordpress: How to programmatically add post for each day?

I would like to know if there is a possibility to have a script that creates automatically daily posts "from today" to "end date" (manually set in the script). 我想知道是否有一种脚本可以自动创建从“今天”到“结束日期”(由脚本手动设置)的每日帖子。 So at each iteration post_date would be $date +1day. 因此,在每次迭代中,post_date将是$ date + 1day。

First i don't know if this script must be executed in functions.php or elsswhere... 首先我不知道该脚本是否必须在functions.php或elsswhere中执行...

Second, i am newbie in php, so i have understood how to create 1 single post with "wp_insert_post" but i don't understand how to insert it in a loop. 其次,我是php的新手,因此我了解了如何使用“ wp_insert_post”创建1个帖子,但我不了解如何将其插入循环中。

Looking for help, if someone has an idea... Thanks a lot 寻求帮助,如果有人有主意...非常感谢

Yes, Of course we can do it. 是的,我们当然可以做到。
From digging through /wp-includes/post.php, It looks like you may need to follow few steps: 通过/wp-includes/post.php的挖掘,您可能需要执行一些步骤:

  1. set your post_status to future. 将您的post_status设置为将来。
  2. set the post_date to when you want it published. 将post_date设置为要发布的日期。
  3. insert the post as your code shows. 如代码所示插入帖子。

     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("Ymd"); $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. Each post will be automatically published on selected date. 每个帖子将在选定的日期自动发布。

  5. This features provided by WordPress. WordPress提供的此功能。

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

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