
Hi guys,
Most posts on this blog are written in batches and scheduled ahead, one per working day. I write them, set a date, and WordPress publishes them in the morning. At least that is the theory. When I moved the blog to hoja.in/blog this year, there were dozens of posts in the status “future”, and while writing the runbook for the go-live I realised I had never really asked myself who publishes them at 07:00. Nobody is logged in at that time. So who presses the button?
The answer is WP-Cron. And WP-Cron is not what the name suggests.
What WP-Cron actually is
A real cron is a daemon on the server that wakes up every minute and runs whatever is due. WP-Cron has no daemon. On every page request, WordPress checks its list of scheduled events (stored in the cron option in the database). If something is due, it fires a small non-blocking HTTP request to wp-cron.php on its own site, and that request runs the due events.
When you schedule a post, WordPress registers a single event for it. You can see it in the core code in _future_post_hook():
wp_schedule_single_event(
strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ),
'publish_future_post',
array( $post->ID )
);At the given time nothing happens by itself. The event only runs when the next visitor arrives. Our hero here is a random reader who opens the blog and, without knowing it, publishes my post.
Problems I faced and my evaluations
For a busy site this is fine, there is always a visitor. For a small personal blog it gets shaky:
- No visitor, no publishing. If nobody comes between 07:00 and noon, the post stays scheduled until noon. In the post list you then see the famous “Missed schedule” label.
- Page caches hide visits. This one surprised me. With a full-page cache like W3 Total Cache, a cached page is often delivered before WordPress fully boots, or without PHP at all. No WordPress, no cron check. The more successful your caching, the less often WP-Cron runs.
- Loopback requests can fail. The spawn request goes from the server to itself. Basic auth on a staging site, a firewall, a wrong DNS entry inside the container, and it silently fails.
- It costs a bit on normal requests. Not much, but every uncached request reads the cron option and does the check.
Honestly, for a blog where the whole point is “publish at 07:00 on a working day”, depending on a visitor felt wrong. So the runbook for the move says: a real cron job on the server. Here is how that looks, step by step.
Solution
Step 1: Tell WordPress to stop spawning WP-Cron on page loads. In wp-config.php, above the “That’s all, stop editing!” line:
define( 'DISABLE_WP_CRON', true );This does not remove the scheduler. Events are still registered and stored. WordPress just stops triggering them on visits. Something else has to do it now.
Step 2: Add a server cron job that runs the due events. With WP-CLI on the server, this is the cleanest way:
*/15 * * * * cd /path/to/webroot/blog && /usr/local/bin/wp cron event run --due-now --quiet >/dev/null 2>&1Use the full path to wp, because cron runs with a very small PATH. And check which PHP binary the CLI uses (wp --info). On shared hosting the command line PHP is sometimes an older version than the web server’s, and then things break in funny ways. In my case the runbook for this site says every CLI step must use PHP 8.4, cron jobs included.
If there is no WP-CLI, calling the URL also works:
*/15 * * * * curl -s 'https://example.org/blog/wp-cron.php?doing_wp_cron' >/dev/null 2>&1I prefer the WP-CLI variant. It does not go through the web server, so no timeouts, no caching layer, no basic auth problems.
Step 3: Pick an interval. Every 15 minutes means a post scheduled for 07:00 appears at the latest at 07:15. For a blog that is more than enough. If you have time-critical things like shop orders or newsletters, go to every 5 minutes.
Step 4: Check that it works. WP-CLI has everything you need:
# What is scheduled, and when?
wp cron event list --fields=hook,next_run_relative,recurrence
# Which posts are waiting?
wp post list --post_status=future --fields=ID,post_date,post_title
# Is the cron system healthy (for setups that still use the spawn)?
wp cron testAfter the first cron run, posts whose date has already passed are published immediately. That is useful after a migration: if you import a database with scheduled posts whose dates are already in the past, the first run catches up.
A small test in DDEV
You can try the behaviour locally without waiting until tomorrow morning. Schedule a test post two minutes ahead, wait, and run the events by hand:
ddev exec wp --path=public/blog post create --post_title='Cron test'
--post_status=future --post_date="$(date -u -v+2M '+%Y-%m-%d %H:%M:%S')"
# two minutes later
ddev exec wp --path=public/blog cron event run --due-now
ddev exec wp --path=public/blog post list --post_status=publish --posts_per_page=1 --fields=ID,post_titleThe date -v+2M syntax is for macOS. On Linux use date -u -d '+2 minutes'. Also keep the blog timezone in mind: this blog runs on UTC, so the post date is UTC too. If your blog uses a local timezone, adjust the date, otherwise you schedule something in the past and it is published right away.
Delete the test post afterwards with wp post delete <ID> --force.
A few things to keep in mind
- Plugins also use WP-Cron: backups, SEO indexing, cache purges. With the real cron job they run on its rhythm now. A plugin that expects “roughly every minute” will run every 15 minutes instead.
- If several servers share the same database, run the cron job on one of them only.
- When you take the site down for maintenance, the cron job keeps running. Most of the time that is harmless, but remember it during database imports.
- Do not forget the server cron when you move hosting. It lives outside the code and outside the database, so it is the one thing a migration silently leaves behind.
That’s it. Five minutes of setup, and scheduled posts no longer wait for a random visitor. If you run WordPress on a server with a lot of cache in front, I’d really recommend doing the same.
Happy coding!