Purge cache when post status changes via cron
-
Hi!
How can I write the equivalent of this function when the post status change is done by cron? The function gets called, but the cache is not cleared. When I change the status of a post in wordpress admin, everything works as expected.function lscwp_suppress_purge_update($new_status, $old_status, $post) { if ($post->post_type == 'my_custom_post') { ob_start(function ($buffer) use ($post) { @header("X-LiteSpeed-Purge: " . LSWCP_TAG_PREFIX . "_Po." . $post->ID); return $buffer; }); } } add_action('transition_post_status', 'lscwp_suppress_purge_update', 10, 3);
-
how did you launch the cron ? by php cli or wget/curl ?
Cron tasks are managed by DirectPanel:
wget -q -O - https://mydomain.com/wp-cron.php >/dev/null 2>&1
So… header(“X-LiteSpeed-Purge: …”) should work?
in theory , yes , you can also hook something like file_put_content() or error_log() to write something to a text file, into that function , see if it actually gets triggered during your cron
Hi @qtwrk, let’s simplify everything. When updating any page, I would like the entire cache to be cleared:
function lscwp_suppress_purge_update($new_status, $old_status, $post) { ob_start(function ($buffer) use ($post) { file_put_contents('purge.log', "OK"); @header("X-LiteSpeed-Purge: *"); return $buffer; }); } add_action('transition_post_status', 'lscwp_suppress_purge_update', 10, 3);
No checkbox is checked on the “Purge -> Auto Purge Rules” (https://docs.litespeedtech.com/lscache/lscwp/cache/#purge-tab).
I update any page. I see “OK” in the purge.log file. All cache is cleared properly. In “Purge Log” (https://docs.litespeedtech.com/lscache/lscwp/toolbox/#log-view-tab) I see:
07/14/23 16:49:15.683 [MY-IP:65224 1 TP8] ?? ------POST HTTP/1.1 (HTTPS) /wp-json/wp/v2/pages/795 07/14/23 16:49:15.683 [MY-IP:65224 1 TP8] Query String: _locale=user 07/14/23 16:49:15.683 [MY-IP:65224 1 TP8] HTTP_REFERER: https://mydomain.com/wp-admin/post.php?post=795&action=edit 07/14/23 16:49:15.683 [MY-IP:65224 1 TP8] User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 07/14/23 16:49:15.683 [MY-IP:65224 1 TP8] Accept: application/json, */*;q=0.1 07/14/23 16:49:15.683 [MY-IP:65224 1 TP8] Accept Encoding: gzip 07/14/23 16:49:15.683 [MY-IP:65224 1 TP8] Cookie _lscache_vary: admin_bar:1;logged-in:1;role:99 07/14/23 16:49:15.683 [MY-IP:65224 1 TP8] X-LSCACHE: true 07/14/23 16:49:15.684 [MY-IP:65224 1 TP8] X-LiteSpeed-Purge: public,8051_Po.795,8051_URL./contact/,8051_W.recent-posts-2,8051_FD => LiteSpeed\LSC->send_headers()@597 => WP_Hook->apply_filters(,ARRAY)@308 => WP_Hook->do_action(ARRAY)@332 => /home/domains/mydomain.com/public_html/wp-includes/load.php@517 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] ?? ------POST HTTP/1.1 (HTTPS) /wp-admin/post.php 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] Query String: post=795&action=edit&meta-box-loader=1&meta-box-loader-nonce=5b115223d3&_locale=user 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] HTTP_REFERER: https://mydomain.com/wp-admin/post.php?post=795&action=edit 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] Accept: application/json, */*;q=0.1 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] Accept Encoding: gzip 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] Cookie _lscache_vary: admin_bar:1;logged-in:1;role:99 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] X-LSCACHE: true 07/14/23 16:49:18.344 [MY-IP:17992 1 ygS] X-LiteSpeed-Purge: public,8051_Po.795,8051_URL./contact/,8051_W.recent-posts-2,8051_FD => LiteSpeed\LSC->send_headers()@597 => WP_Hook->apply_filters(,ARRAY)@308 => WP_Hook->do_action(ARRAY)@332 => /home/domains/mydomain.com/public_html/wp-includes/load.php@517
Now I’m using the “PublishPress Future” plugin (https://www.ads-software.com/plugins/post-expirator/) to automatically turn the selected page into a “draft” at 4:50 PM. I delete the “purge.log” file and wait… The selected page turns into a draft and in the “purge.log” file I see “OK”. Unfortunately, the cache for all other pages is not cleared. In “Purge Log” I see:
07/14/23 16:50:03.464 [SERVER-IP:56594 1 8hr] ?? ------GET HTTP/1.1 (HTTPS) /wp-cron.php 07/14/23 16:50:03.465 [SERVER-IP:56594 1 8hr] Query String: 07/14/23 16:50:03.465 [SERVER-IP:56594 1 8hr] User Agent: Wget/1.14 (linux-gnu) 07/14/23 16:50:03.465 [SERVER-IP:56594 1 8hr] Accept: */* 07/14/23 16:50:03.465 [SERVER-IP:56594 1 8hr] Accept Encoding: gzip 07/14/23 16:50:03.465 [SERVER-IP:56594 1 8hr] X-LSCACHE: true 07/14/23 16:50:03.465 [SERVER-IP:56594 1 8hr] X-LiteSpeed-Purge: public,8051_Po.795,8051_W.recent-posts-2,8051_FD => LiteSpeed\LSC->send_headers()@597 => WP_Hook->apply_filters(,ARRAY)@308 => WP_Hook->do_action(ARRAY)@332 => /home/domains/mydomain.com/public_html/wp-includes/load.php@517
My goal is to achieve this situation: if the selected page automatically turns into a “draft” then clear the cache for a few selected pages. Unfortunately, the cache does not clear when the action is called by cron.
when you use that header() function, it is not linked to plugin thus no debug log shows up, to get log you need to use plugin API to purge all
try use plugin API to purge all and then see what log says
@qtwrk what API do you mean? Can you send a link to the documentation?
Now it works like a dream! Thank you ??
Hi @qtwrk,
Unfortunately, I’m still having some problems. Sometimes the page cache clears and sometimes it doesn’t. I’ve been trying to find the cause for several days. The site works in “wordpress multisite” mode (subdomains mapping to domain)
- mydomain.com (main site)
- site1.mydomain.com -> site1.com
- site2.mydomain.com -> site2.com
- etc.
Cron is called by the server every minute, for each domain independently:
wget -q -O – https://mydomain.com/wp-cron.php >/dev/null 2>&1
wget -q -O – https://site1.com/wp-cron.php >/dev/null 2>&1
wget -q -O – https://site2.com/wp-cron.php >/dev/null 2>&1
etc.So far I have not noticed problems on the main site. On the other hand, on other pages the cache once clears and once does not. This is what the code looks like in simplified form:
function lscwp_suppress_purge_update($new_status, $old_status, $post) { file_put_contents('purge.log', 'OK:' . get_current_blog_id()); do_action('litespeed_purge_post', '935'); } add_action('transition_post_status', 'lscwp_suppress_purge_update', 10, 3);
In the purge.log file, I can see that the code fires correctly every time (also the blog ID is saved correctly). I have turned on the “debug” mode and below are the logs that also appear every time:
08/08/23 01:14:02.525 [SERVER-IP:33784 6 JLG] ?? ------GET HTTP/1.1 (HTTPS) /wp-cron.php 08/08/23 01:14:02.525 [SERVER-IP:33784 6 JLG] Query String: 08/08/23 01:14:02.525 [SERVER-IP:33784 6 JLG] User Agent: Wget/1.14 (linux-gnu) 08/08/23 01:14:02.525 [SERVER-IP:33784 6 JLG] Accept: */* 08/08/23 01:14:02.525 [SERVER-IP:33784 6 JLG] Accept Encoding: gzip 08/08/23 01:14:02.525 [SERVER-IP:33784 6 JLG] X-LSCACHE: true 08/08/23 01:14:02.526 [SERVER-IP:33784 6 JLG] X-LiteSpeed-Purge: public,07a6_Po.935,07a6_URL./somepage/,07a6_W.recent-posts-2,07a6_FD,07a6_REST,07a6_Po.36176 => LiteSpeed\LSC->send_headers()@597 => WP_Hook->apply_filters(,ARRAY)@308 => WP_Hook->do_action(ARRAY)@332 => /home/domains/mydomain.com/public_html/wp-includes/load.php@517
Source page:
<!-- Page generated by LiteSpeed Cache 5.3.2 on 2023-08-08 00:11:13 --> <!-- X-LiteSpeed-Cache-Control: public,max-age=604800 --> <!-- X-LiteSpeed-Tag: 07a6_HTTP.200,07a6_page,07a6_URL./somepage/,07a6_Po.935,07a6_PGS,07a6_ -->
Nevertheless, sometimes the page is not refreshed (it is still being cached). Once that happens, no matter how many times this code is fired – the page won’t clean up anymore. Only calling the action via the browser (not via cron) will clear the cache. I also tried do_action( ‘litespeed_purge_url’, ‘the_url’ ); but there is the same problem.
- site1.mydomain.com -> site1.com
- site2.mydomain.com -> site2.com
what does this mean ? you have 4 different domains here , like visitor sees
site1/2.com
in front , but you manage them assite1/2.mydomain.com/wp-admin
?em , so you basically have 4 virtual host ?
One hosting, one directory with wordpress. Multiple domains point to the same directory.
I don’t know if the problem is related to “multisite” or the fact that the action is called by cron, or maybe both? In any case, your plugin supports multisite, which is mentioned many times in the documentation, e.g.:
https://docs.litespeedtech.com/lscache/lscwp/cache/#enable-cache
For single site installations, only
ON
andOFF
are available. For Multisite Subsite admins, there is a third option,Use Network Admin Setting
. This last option uses what the Network Admin chooses.Maybe someone could help with multisite experience?
okay , please create a ticket by mail to
support at litespeedtech.com
with reference link to this topic , we will investigate further.
- The topic ‘Purge cache when post status changes via cron’ is closed to new replies.