Lufy
Lufy

Reputation: 27

wp_insert_post auto schedule or post based on date

<?php 
//today date 2014-07-27
$title="title";
$content="content";
$date="2014-07-30"; 

    $my_post = array(
        'post_title'        => $title,
        'post_content'      => $content,
        'post_status'       => 'post',
        'post_date'         => $date,
        'post_author'       => 1
    );
    $post_id = wp_insert_post( $my_post );
 ?>

how to make post auto post when the date today (or older day) and auto schedule when the date tomorrow ?

ps: for today (or older day) maybe that code will work correctly but how to do it for auto schedule when i put the date tomorrow or next day (if it possible please give sample code)

update question: what i mean is how to make wp_insert_post make a schedule post when the date set to the future (next day/next month/next year or specific date) because when i try to set 'post_date' => "2015-08-30" the post keep post with today date what i want is that post will create as schedule post

thanks

Upvotes: 0

Views: 2911

Answers (2)

Magicianred
Magicianred

Reputation: 566

The post datetime is [ Y-m-d H:i:s ], try it:

<?php
$timezone_offset = get_option( 'gmt_offset' );
$post_date = date_create( "2014-07-30" );
$post_date_gmt = date_create( "2014-07-30" );
$post_date_gmt->add( new DateInterval( "PT{$timezone_offset}H" ) );

//today date 2014-07-27
$title="title";
$content="content";
$date="2014-07-30"; 

$my_post = array(
  'post_title'        => $title,
  'post_content'      => $content,
  'post_status'       => 'post',
  'post_date'      => $post_date->format('Y-m-d H:i:s'), 
  'post_date_gmt'  => $post_date_gmt->format('Y-m-d H:i:s'), 
  'post_author'       => 1
);
$post_id = wp_insert_post( $my_post );
?>

Enjoy your code

Upvotes: 0

SpencerX
SpencerX

Reputation: 5723

To test if the date is for tomorrow, try something like this :

$tomorrow = date('Y-m-d', strtotime('tomorrow')); 

And for the day after tomorrow :

$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));

Then you can test $date:

if ($date == $tomorrow) {
  echo "tomorrow";
} elseif ($date == $day_after_tomorrow) {
  echo "dayaftertomorrow";
}

Updated answer :

Basically post_date is for the time post was made and if you want hack i think that you need to have date in this format : [ Y-m-d H:i:s ]

Upvotes: 1

Related Questions