简体   繁体   中英

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.

First i don't know if this script must be executed in functions.php or 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.

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:

  1. set your post_status to future.
  2. set the post_date to when you want it published.
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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