I am a very strange problem. I am using Wordress 3.9.1 and in an admin’s plugin i have this code:
function do_this_event_now() {
for ($i = 0; $i < 5; $i++)
{
$my_post = array(
'post_title' => 'My post: '. date('Y-m-d H:i:s'),
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(1)
);
// Insert the post into the database
wp_insert_post( $my_post );
sleep(50);
}
}
add_action( 'my_new_event', 'do_this_event_now' );
wp_schedule_single_event( time(), 'my_new_event' );
It should create a post each 50 seconds. But the result is very strange.
I correctly see 5 posts but the titles ( where i write the date('Y-m-d H:i:s')
) are really wrong:
My post: 2014-07-10 15:14:57
My post: 2014-07-10 15:15:47
My post: 2014-07-10 15:16:01
My post: 2014-07-10 15:16:38
My post: 2014-07-10 15:16:51
As you can see the differences are not 50 seconds!
How is this possible? This is a single event why this weird result?
Thank you!
Unlike the cron that most systems administrators are used to, WP-Cron isn’t based off the server’s clock, or even the server itself. Instead, thereâs a check every time a person calls one of your pages or posts. Every time WordPress starts to render a page, it checks to see if there are any jobs that need to be run with WP-Cron. If your site doesn’t get visited very often (like most development sites), your posts will not be created exactly every 50 seconds. You can change this by following a guide like this: http://www.kettlewellconsulting.com/2013/06/23/reliably-schedule-wordpress-tasks/ to turn off the default WP-Cron and implement your own cron job, so it is constantly creating those posts as you would like it to.