WpW: Using LSCache for WP with Scheduled Posts

May 17th, 2017 by LSCache 6 Comments

Welcome to the first installment of WordPress Wednesday!

Today we’re going to be talking about Scheduled Posts. In particular, we’ll be discussing why your scheduled posts may not appear on time if you have LiteSpeed Cache (or another caching solution) installed.

So you’ve installed LiteSpeed Cache, and your WordPress site is zipping along beautifully. But there is a problem: the cache is interfering with the cron, and your scheduled posts are being published late.

Some WordPress features, including post scheduling, rely on WP-Cron to work properly. When your site is fully-cached (whether through our plugin or some other means), WP-Cron can be prevented from running. Read on to learn why this is, and how to configure your system so that Wp-Cron, Scheduled Posts, and LiteSpeed Cache can all live together in harmony.

There are many reasons why you might want to schedule a post. Maybe you are going on vacation, but you want there to be a steady stream of new content published to your blog while you are gone. Maybe you like to have all of your content posted on a Monday, but you like to do the actual writing on Fridays. Maybe your audience expects a daily post at a consistent 6am on weekdays, but you enjoy sleeping in until 9 some mornings.

Whatever the reason, when you schedule your posts for a particular time, you would like them to actually post at that time!

Sometimes, though, scheduled posts and caching don’t get along.

The key to understanding why, is in the WordPress cron.

You might expect that a cron job, scheduled to run at 6am, would fire as soon as the system clock hits 6am. WP-Cron, though, isn’t tied to the system clock. It is triggered by visitor requests. If your small blog sits there quietly attracting no visitors for four hours, then your WP-Cron job is not going to be triggered until your first visitor shows up, four hours later.

On the other hand, if you have a reasonable amount of traffic, and no cache enabled, your cron jobs will be triggered pretty close to their specified times, and you’re not likely to notice if they’re delayed by a minute or so.

Now think about how LiteSpeed and other caches work. When your blog pages are cached, there is no reason to invoke WordPress to do any work. No matter how busy your blog may be, WP remains idle while the cache serves pages to your visitors. And with LiteSpeed Cache, which can do accurate purging by triggered events, public TTL may be set to a really long time. Even a busy site could appear idle for 24 hours or even a week! This is excellent, desired behavior from a site-performance perspective (It’s true – check it out), but it can be a problem when your publishing schedule relies on WP-Cron.

If WordPress doesn’t wake up, neither does its cron. This is what causes scheduled posts to publish late.

So what can we do to make the cache and the cron play nice?

In order to make sure that WP-Cron runs regularly on cached sites, you can manually schedule a server-level cron job to invoke wp-cron.php at your ideal interval. Here’s how:

To force WP-Cron to check itself every 15 minutes, enter crontab -e at the command-line prompt, and add the following line to the file:

*/15 * * * * wget http://your_wp_site/wp-cron.php

This is literally read as “execute wget http://your_wp_site/wp-cron.php at 15-minute intervals every hour, every day of the month, every month, every day of the week.” It means your site will no longer be dependent on visitors and non-cached content to trigger your scheduled posts. WP-Cron will be guaranteed to run every 15 minutes.

Another example: Let’s go back to that idea of having all of your posts scheduled for weekdays at 6:00 am. If this is the only truly critical time for you and your cron, add this line via the crontab -e command:

01 6 * * 1,2,3,4,5 wget http://your_wp_site/wp-cron.php

This is read as “execute wget http://your_wp_site/wp-cron.php the first minute of the 6th hour every day of the month, every month, on the 1st, 2nd, 3rd, 4th, and 5th days of the week.” It lets WordPress handle the cron most of the time, but also has your system manually trigger wp-cron.php once a day at 6:01am. Just in case.

To choose your own interval, you can modify the line above according to this pattern:

minute hour day-of-month month day-of-week wget http://your_wp_site/wp-cron.php

For minute, hour, day-of-month, month, and day-of-week, you can use the following:

  • A number: 10 just means 10 (10th minute, 10th hour, 10th day of the month, etc.)
  • An interval: */10 represents an interval of 10 (so for minutes, it would be :00, :10, :20, :30, and so on)
  • A list: 10,20,30 represents a list with 3 values (so for minutes, it would be :10, :20, and :30)
  • A wildcard: * represents all of them (every minute, every hour, every day, etc.)

(Please note that for day-of-week, 0=Sunday, 1=Monday, 2=Tuesday, and so on.)

Now that we’ve ensured the cron will run on an appropriate schedule, let’s go back to our 15-minute example.

At this point, WP-Cron is being triggered every quarter of an hour by the system cron, but it is also still triggered by visitor requests. We could leave it this way, but if the 15 minute thing is sufficient for your purposes, and you want to avoid extra load on your server, you can disable the default visitor-triggered behavior.

Simply add this line to your wp-config.php file:

define('DISABLE_WP_CRON', true);

(If your config file already has a line that reads define('DISABLE_WP_CRON', false); then don’t add a new line. Simply change false to true.)

And there you have it. WP-Cron now triggers 4 times an hour, every 15 minutes, no matter how many visitors you have or how many pages are being cached. Schedule your posts around the 15-minute mark of any hour (:00, :15, :30, :45) and they will never be posted late again!

By the way, if you’ve read this far and you’re not using LiteSpeed Cache with your WordPress blog, why the heck not? You can download it here, or get more information in the links below.

P.S. As of this writing, we’ve got more than 80,000 active installs – that’s 10,000 up since our post last week!

Have any questions about this? Have some of your own ideas for future WordPress Wednesday topics? Leave us a comment!

Don’t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:


Related Posts