Trigger WordPress's missed schedule check on every front-end page load so scheduled posts publish reliably on shared hosting where WP-Cron doesn't fire consistently.
add_action( 'init', function (): void {
// Check at most once every five minutes.
if ( get_transient( 'nahnu_missed_schedule_check' ) ) {
return;
}
set_transient( 'nahnu_missed_schedule_check', 1, 5 * MINUTE_IN_SECONDS );
$missed = get_posts( [
'post_type' => 'any',
'post_status' => 'future',
'date_query' => [ [ 'column' => 'post_date_gmt', 'before' => gmdate( 'Y-m-d H:i:s' ) ] ],
'posts_per_page' => 10,
'fields' => 'ids',
'no_found_rows' => true,
] );
foreach ( $missed as $post_id ) {
check_and_publish_future_post( $post_id );
}
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WordPress’s scheduled post system relies on WP-Cron, a pseudo-cron that triggers on page loads. When a visitor hits your site around the scheduled time, WP-Cron fires and publishes any due posts. On shared hosting environments with low traffic, particularly during overnight or early morning scheduling windows, it’s common for no visitor to arrive at exactly the right time, meaning scheduled posts simply don’t publish until the next page load after their scheduled time. High-traffic periods or server-side cron jobs solve this, but this snippet provides a lightweight fix for lower-traffic shared hosting environments.
It checks for missed scheduled posts on page load, but uses a transient to limit the database check to once every five minutes rather than every request.
Running a database query on every page load to check for missed posts would add measurable overhead at scale. The transient nahnu_schedule_check acts as a gate, once the check runs, it sets a transient that expires in five minutes. Every subsequent request within that window hits the transient check (fast, in-memory on sites with object cache) and skips the query entirely. Only after five minutes does the full query run again.
Five minutes is a reasonable interval for most publishing workflows. If you need posts to publish within a tighter window, reduce the interval. If your hosting environment is particularly constrained, increase it to reduce database load.
The query finds posts with status future, WordPress’s status for scheduled posts, whose post_date is in the past. These are posts that were supposed to publish but didn’t. posts_per_page => 5 limits the batch size to prevent a large backlog from causing a slow request. fields => 'ids' retrieves only post IDs rather than full post objects, reducing query overhead.
wp_publish_post() changes the post status to publish and triggers all associated actions, post published notifications, cache clearing, and any hooks other plugins have registered for post publication.
For production sites, the correct fix for WP-Cron reliability is to disable WP-Cron’s page-load trigger and set up a real system cron job instead. Add define( 'DISABLE_WP_CRON', true ); to wp-config.php and configure a server cron to hit yourdomain.com/wp-cron.php every five minutes using curl or wget. This snippet is the fallback for environments where system cron access isn’t available.