As Rarst mentioned I had to use wp_update_post()
, but then there is a little trick – one must also set edit_date = true
otherwise it has funny behavior. The final code looks like this:
function schedule() {
$postdate = date('2014-06-11 01:00:00');
$postdate_gmt = date('2014-06-11 05:00:00');
$post = array(
'ID' => 11,
'post_status' => 'future',
'post_type' => 'post',
'post_author' => '1',
'ping_status' => 'closed',
'to_ping' => 'http://rpc.pingomatic.com/',
'post_date_gmt' => $postdate_gmt,
'post_date' => $postdate,
'edit_date' => 'true'
);
wp_update_post( $post, true );
}
add_action('wp_head', 'schedule');
And here’s how it can work for many posts, using a text file:
Text File:
postid,server_time,gmt_time
postid,server_time,gmt_time
postid,server_time,gmt_time
postid,server_time,gmt_time
...
Function:
function schedule() {
$fh = @fopen( dirname( __FILE__ ) . '/schedule.txt', 'r' );
if ( $fh ) {
while ( ( $line = fgets( $fh ) ) !== false ) {
$ids = explode( ',', $line );
array_walk( $ids, 'trim' );
$postdate = date($ids[1]);
$postdate_gmt = date($ids[2]);
$post = array(
'ID' => $ids[0],
'post_status' => 'future',
'post_type' => 'post',
'post_author' => '5',
'ping_status' => 'closed',
'to_ping' => 'http://rpc.pingomatic.com/',
'post_date_gmt' => $postdate_gmt,
'post_date' => $postdate,
'edit_date' => 'true'
);
wp_update_post( $post, true );
}
}
}
add_action('wp_head', 'schedule');
Many thanks to everyone who helped in this and other threads!