Flushing the RSS cache in WordPress
Published September 3rd, 2013 under General
Another update (2017-12-02): I tried using this method again, and discovered that the second version also no longer works. The following can now be used to stop your feeds from being cached.
function turn_off_feed_caching( $feed ) { $feed->enable_cache( false ); } add_action( 'wp_feed_options', 'turn_off_feed_caching' );
Update (some time around 2015): Rarst just mentioned to me that the Magpie library has been deprecated within WordPress core, and so this is no longer the correct way to flush the RSS cache in WordPress. Instead, you should use the following code.
<?php /** * Setting a new cache time for feeds in WordPress */ function prefix_set_feed_cache_time( $seconds ) { return 1; } add_filter( 'wp_feed_cache_transient_lifetime' , 'prefix_set_feed_cache_time' ); ?>
Below is the original post demonstrating the Magpie code.
For the past few days I’ve been driven mildly nuts from dealing with the WordPress feed system. I should have thought of this earlier, but what was causing most of my confusion was that WordPress internally caches the RSS feeds it reads into it. I tracked down the solution whilst rifling through the wp-includes/rss.php file. In there is a definition for the cache age which is set by default to refresh once every hour.
Here is the code from the rss.php file:
<?php if ( !defined('MAGPIE_CACHE_AGE') ) { define('MAGPIE_CACHE_AGE', 60*60); // one hour }
You can override this via your wp-config.php file by declaring the cache age definition before rss.php has run.
<?php // Place the following line in your wp-config.php file ... define( 'MAGPIE_CACHE_AGE', 10 ); // Cache RSS feeds for only 10 seconds
Scott says:
This doesn\’t seem to work for me, at least in WP 4.1. I\’m also using WP Super Cache, but have also deleted my cache each time.
January 12, 2015 at 7:40 pm # //
Ryan says:
I’m not sure sorry 🙁
If you find a solution, please let me know and I’ll update this post to whatever the improved solution is.
January 13, 2015 at 9:21 am # //
Aj Clarke says:
Nice, thanks for posting. I was wondering why the feed wasn\’t changing when using the the_excerpt_rss filter 😉
June 10, 2015 at 10:26 pm # //
Ryan says:
Glad you found this old post useful 🙂
June 23, 2015 at 2:55 pm # //
Jeffrey van Rossum says:
To change the time of feed caching you can use the wp_feed_cache_transient_lifetime-filter.
https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_feed_cache_transient_lifetime
August 12, 2015 at 7:18 am # //
Ryan says:
Yes, that is what is in the updated instructions at the top of the post 😉
August 12, 2015 at 7:42 am # //
zeeshan naqi says:
Hello,
I am a newbie at this, I am having some issues with Apple News app. and their techs told me:
The cache control header of your RSS feed sets expiration at 172800 seconds, or 48 hours.
To resolve this issue, engineering is recommending that you change the Cache-Control header to *not* say to cache the RSS feed for up to 48 hours.
I have no idea what to do with it. Can you please help?
Thank you.
August 3, 2016 at 7:53 pm # //
Ryan says:
Then just change where it says “1” in the snippet above to say 48 hours (in seconds).
August 8, 2016 at 8:00 pm # //
Tornado BD says:
I am using Wp super cache, Please tell me I have to cache the feeds or not?
December 4, 2016 at 7:35 am # //
Ryan says:
I have no idea whether you have to cache your feeds or not. That is a decision for you to make yourself.
December 4, 2016 at 4:27 pm # //
TechPrevue says:
I have changed value 43200 to 10 but cache is old. How to flush it?
August 20, 2017 at 4:33 pm # //
Ryan says:
You seem to have missed the bit at the top of the page which says this code has been deprecated.
August 20, 2017 at 5:09 pm # //
Techgape says:
Very useful, thanks.
January 26, 2018 at 4:25 pm # //
tao says:
what about changing the cache time instead of disabling it?
Is disabling cache a risky thing to do? Does it put heavy load on a server?
May 21, 2018 at 9:02 pm # //
Ryan Hellyer says:
If it’s cached further up the stack it would be fine. My own feeds are cached via Varnish, so it wouldn’t bother me to totally disable the cache on them. But if you had no caching in place, the feed loading could easily overload the server.
May 21, 2018 at 10:02 pm # //