{"id":7045,"date":"2017-05-17T06:00:39","date_gmt":"2017-05-17T10:00:39","guid":{"rendered":"http:\/\/blog.litespeedtech.com\/?p=7045"},"modified":"2017-05-17T06:00:39","modified_gmt":"2017-05-17T10:00:39","slug":"wpw-using-lscache-for-wp-with-scheduled-posts","status":"publish","type":"post","link":"https:\/\/lswp.store\/index.php\/2017\/05\/17\/wpw-using-lscache-for-wp-with-scheduled-posts\/","title":{"rendered":"WpW: Using LSCache for WP with Scheduled Posts"},"content":{"rendered":"<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-7533\" src=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw02-1.jpg\" alt=\"\" width=\"640\" height=\"320\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw02-1.jpg 640w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw02-1-300x150.jpg 300w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw02-1-600x300.jpg 600w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>Welcome to the first installment of <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\">WordPress Wednesday<\/a>!<\/p>\n<p>Today we\u2019re going to be talking about Scheduled Posts. In particular, we\u2019ll be discussing why <strong>your scheduled posts may not appear on time if you have <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\">LiteSpeed Cache<\/a> (or another caching solution) installed.<\/strong><\/p>\n<p>So you&#8217;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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Whatever the reason, when you schedule your posts for a particular time, you would like them to <em>actually post at that time!<\/em><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/blog.litespeedtech.com\/wp-content\/uploads\/2017\/05\/09_time_01_ed-copy-1024x683.jpg\" alt=\"\" width=\"1024\" \/><\/p>\n<p><strong>Sometimes, though, scheduled posts and caching don\u2019t get along.<\/strong><\/p>\n<p>The key to understanding why, is in the WordPress cron.<\/p>\n<p>You might expect that a cron job, scheduled to run at 6am, would fire as soon as the system clock hits 6am. <a href=\"https:\/\/developer.wordpress.org\/plugins\/cron\/\">WP-Cron, though, <em>isn\u2019t tied to the system clock<\/em>. It is triggered by visitor requests.<\/a> 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, <em>four hours later<\/em>.<\/p>\n<p>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\u2019re not likely to notice if they\u2019re delayed by a minute or so.<\/p>\n<p>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, <em>WP remains idle while the cache serves pages to your visitors.<\/em> And with <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\">LiteSpeed Cache<\/a>, which can do accurate purging by triggered events, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Time_to_live\">public TTL<\/a> may be set to a <em>really<\/em> 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\u2019s true &#8211; <a href=\"https:\/\/www.litespeedtech.com\/benchmarks\/wordpress\">check it out<\/a>), but it can be a problem when your publishing schedule relies on WP-Cron.<\/p>\n<p>If WordPress doesn\u2019t wake up, neither does its cron. <strong>This is what causes scheduled posts to publish late<\/strong>.<\/p>\n<p>So what can we do to make the cache and the cron play nice?<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/blog.litespeedtech.com\/wp-content\/uploads\/2017\/05\/09_time_04_ed-copy-1024x683.jpg\" alt=\"\" width=\"1024\" \/><\/p>\n<p>In order to make sure that WP-Cron runs regularly on cached sites, you can <strong>manually schedule a server-level cron job<\/strong> to invoke <code>wp-cron.php<\/code> at your ideal interval. Here\u2019s how:<\/p>\n<p>To force WP-Cron to check itself every 15 minutes, enter <code>crontab -e<\/code> at the command-line prompt, and add the following line to the file:<\/p>\n<pre>*\/15 * * * * wget http:\/\/your_wp_site\/wp-cron.php\n<\/pre>\n<p>This is literally read as \u201cexecute <code>wget http:\/\/your_wp_site\/wp-cron.php<\/code> at 15-minute intervals every hour, every day of the month, every month, every day of the week.\u201d It means <em>your site will no longer be dependent on visitors and non-cached content to trigger your scheduled posts.<\/em> WP-Cron will be guaranteed to run every 15 minutes.<\/p>\n<p><strong>Another example<\/strong>: Let\u2019s 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 <code>crontab -e<\/code> command:<\/p>\n<pre>01 6 * * 1,2,3,4,5 wget http:\/\/your_wp_site\/wp-cron.php\n<\/pre>\n<p>This is read as \u201cexecute <code>wget http:\/\/your_wp_site\/wp-cron.php<\/code> 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.\u201d 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.<\/p>\n<p>To choose your own interval, you can modify the line above according to this pattern:<\/p>\n<pre>minute hour day-of-month month day-of-week wget http:\/\/your_wp_site\/wp-cron.php\n<\/pre>\n<p>For minute, hour, day-of-month, month, and day-of-week, you can use the following:<\/p>\n<ul>\n<li>A number: <code>10<\/code> just means 10 (10th minute, 10th hour, 10th day of the month, etc.)<\/li>\n<li>An interval: <code>*\/10<\/code> represents an interval of 10 (so for minutes, it would be :00, :10, :20, :30, and so on)<\/li>\n<li>A list: <code>10,20,30<\/code> represents a list with 3 values (so for minutes, it would be :10, :20, and :30)<\/li>\n<li>A wildcard: <code>*<\/code> represents all of them (every minute, every hour, every day, etc.)<\/li>\n<\/ul>\n<p>(Please note that for day-of-week, 0=Sunday, 1=Monday, 2=Tuesday, and so on.)<\/p>\n<p>Now that we\u2019ve ensured the cron will run on an appropriate schedule, let\u2019s go back to our 15-minute example.<\/p>\n<p>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 <strong>disable the default visitor-triggered behavior<\/strong>.<\/p>\n<p>Simply add this line to your wp-config.php file:<\/p>\n<pre>define('DISABLE_WP_CRON', true);\n<\/pre>\n<p>(If your config file already has a line that reads <code>define('DISABLE_WP_CRON', false);<\/code> then don\u2019t add a new line. Simply change <code>false<\/code> to <code>true<\/code>.)<\/p>\n<p>And there you have it. <strong>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<\/strong>. Schedule your posts around the 15-minute mark of any hour (:00, :15, :30, :45) and they will never be posted late again!<\/p>\n<p>By the way, if you\u2019ve read this far and you\u2019re not using LiteSpeed Cache with your WordPress blog, <em>why the heck not<\/em>? You can download it <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\">here<\/a>, or get more information in the links below.<\/p>\n<p>P.S. As of this writing, we\u2019ve got more than 80,000 active installs &#8211; that\u2019s 10,000 up since <a href=\"http:\/\/blog.litespeedtech.com\/2017\/05\/10\/wordpress-wednesday\/\">our post<\/a> last week!<\/p>\n<p>&#8212;<\/p>\n<p>Have any questions about this? Have some of your own ideas for future <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\">WordPress Wednesday<\/a> topics? Leave us a comment!<\/p>\n<p>Don\u2019t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:<\/p>\n<ul>\n<li>Subscribe to the <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\/feed\/\">WordPress Wednesday RSS feed<\/a><\/li>\n<li>Download <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\">LiteSpeed Cache for WordPress plugin<\/a><\/li>\n<li>Learn more about the plugin <a href=\"https:\/\/www.litespeedtech.com\/products\/cache-plugins\/wordpress-acceleration\">on our website<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>So you&#8217;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. Here&#8217;s how to solve that.<\/p>\n","protected":false},"author":1,"featured_media":8142,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28],"tags":[84,316,317,320],"class_list":["post-7045","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cache","tag-cron","tag-wordpress","tag-wordpress-wednesday","tag-wp-cron"],"jetpack_featured_media_url":"https:\/\/lswp.store\/wp-content\/uploads\/2017\/07\/wpw02.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts\/7045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/comments?post=7045"}],"version-history":[{"count":0,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts\/7045\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/media\/8142"}],"wp:attachment":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/media?parent=7045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/categories?post=7045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/tags?post=7045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}