WordPress misses Publishing Schedule

Feb 08 2013 Published by under Wordpress

I haven’t figured out why WordPress is missing schedule post, but I wrote a script that is ran with crontab that notifies me when it happens.

<?php

include 'wp-load.php';

$total_missed_schedules = 0;

$blogs = crontab_get_blogs();

$output = '';

foreach($blogs as $blog) {
	$blog_prefix = $wpdb->get_blog_prefix( $blog->blog_id );

	$results = query_missed_schedules( $blog_prefix );

	$total_missed_schedules += count($results);

	$output .= ($blog->domain . ' (blog id:' . $blog->blog_id . ') missed schedules ' . count($results) . '<br />' . PHP_EOL);

	if ($results) {

		switch_to_blog( $blog->blog_id );

		foreach ( $results as $row ) {
			$output .= ("post_id: <a href='" . get_edit_post_link($row->ID) . "'>" . $row->ID . "</a> date: " . $row->post_date . " post_type: " . $row->post_type . "<br />" . PHP_EOL);
		}

		restore_current_blog();
	}
}

echo $output;

if ($total_missed_schedules > 0 || isset($_GET['mailtest'])) {

	$to = 'matt@thiessen.us';
	$subject = 'Missed scheduled posts';
	$text = '';

	if(isset($_GET['cron_src'])) {
		$text = 'cron ran from ' . $_GET['cron_src'];
		$text .= '<br />wget http://thegazette.com/missed_pub_notifier.php<br /><br />';
	}

	add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
	wp_mail($to, $subject . " (wp_mail)", $text . $output);
}

function query_missed_schedules( $blog_prefix ) {
	global $wpdb;

	$sql = "SELECT ID, post_date, post_date_gmt, post_type, post_title FROM " . $blog_prefix . "posts WHERE post_status = 'future' AND post_date_gmt <= UTC_TIMESTAMP();";

	$results = $wpdb->get_results($sql);

	return $results;
}

function crontab_get_blogs() {
	global $wpdb;

	$sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE deleted = 0 AND archived = '0' ORDER BY domain, path";
	$results = $wpdb->get_results($sql);
	return ($results);
}

?>

One response so far

You must be logged in to post a comment.