Created
September 14, 2017 04:58
-
-
Save lesterchan/02ea47a3456340997082d765e33ba67b to your computer and use it in GitHub Desktop.
Fix missed WordPress scheduled post by publishing it after it has been missed (PHP 7.1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Fix for missing scheduled post | |
* | |
* @param array $schedules Cron Schedules. | |
* | |
* @return array | |
*/ | |
function lc_cron_schedules( array $schedules ) : array { | |
$schedules['lc_publish_missed_scheduled_posts'] = [ | |
'interval' => ( 5 * MINUTE_IN_SECONDS ), | |
'display' => 'Publish Missed Scheduled Posts', | |
]; | |
return $schedules; | |
} | |
add_filter( 'cron_schedules', 'lc_cron_schedules' ); | |
/** | |
* Fix for missing scheduled post | |
* | |
* @return void | |
*/ | |
function lc_setup_publish_missed_scheduled_posts() : void { | |
if ( ! wp_next_scheduled( 'lc_cron_publish_missed_scheduled_posts' ) ) { | |
wp_schedule_event( time(), 'lc_publish_missed_scheduled_posts', 'lc_cron_publish_missed_scheduled_posts' ); | |
} | |
} | |
add_action( 'wp', 'lc_setup_publish_missed_scheduled_posts' ); | |
/** | |
* Fix for missing scheduled post | |
* | |
* @return void | |
*/ | |
function lc_publish_missed_scheduled_posts() : void { | |
global $wpdb; | |
$scheduled_post_ids = $wpdb->get_col( | |
$wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ( ( post_date > 0 && post_date <= %s ) ) AND post_status = 'future' LIMIT 0, 10", current_time( 'mysql' ) ) | |
); | |
if ( ! count( $scheduled_post_ids ) ) { | |
return; | |
} | |
foreach ( $scheduled_post_ids as $scheduled_post_id ) { | |
if ( ! $scheduled_post_id ) { | |
continue; | |
} | |
wp_publish_post( $scheduled_post_id ); | |
} | |
} | |
add_action( 'lc_cron_publish_missed_scheduled_posts', 'lc_publish_missed_scheduled_posts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment