Refresh fetch_feed cache

Refresh fetch_feed cache

By Jonathan on March 27, 2012

If like us you are using the inbuilt WordPress ‘fetch_feed’ function you might come across some issues with regards to the posts showing up, or showing correctly if they have been edited. The ‘fetch_feed’ function actually only checks the RSS every 12hours, which for some websites is often enough, however for us this seemed like a long time and we wanted to also force the RSS feed to be updated.

We originally thought that the RSS feed was stored via the browser cache, however when looking into this we noticed that this was not the case. We also tried to clear the browser cache and this did not work either.

WordPress actually holds the RSS feed in its own cache, thankfully because of this we are able to add a filter to our functions.php file to force a refresh.

{code type=php}
add_filter( ‘wp_feed_cache_transient_lifetime’, create_function( ‘$a’, ‘return 1;’ ) );
{/code}

Above we are able to add a filter into the wp_feed_cache_transient_lifetime function and change the duration the feed is kept in the cache for. We use return 1 to say that we only want the cache to last for 1 second before it is checked again. Using this will force the cache to be refreshed. Once we have forced the cache to refresh immediately we can change this to something more suitable, such as 1 hour, as seen below.

{code type=php}
add_filter( ‘wp_feed_cache_transient_lifetime’, create_function( ‘$a’, ‘return 3600;’ ) );
{/code}

This filter can go anywhere within the functions.php file.


Falkon blog post featured image for post - WordPress Templates

WordPress Templates

Ever wanted to customise a single page in WordPress with the template system but never knew how? Its simple with...