Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

According to the docs, if you want to cache results for a fetch of RSS feeds with simplepie, you do this:

 add_filter( 'wp_feed_cache_transient_lifetime' , 'return_7200' );
 $feed = fetch_feed( $feed_url );
 remove_filter( 'wp_feed_cache_transient_lifetime' , 'return_7200' );

My question is if I want to cache the results for several feed urls (by looping through an array), would I really want to add and remove the filter for each pass of the loop (wouldn't that cause separate entries in the cache for each feed) or should I do this:

add_filter( 'wp_feed_cache_transient_lifetime' , 'return_7200' );

for ($i=.......){
  $feed = fetch_feed( $feed_url );
}

 remove_filter( 'wp_feed_cache_transient_lifetime' , 'return_7200' );

Frankly (and this might be obvious), I'm confused as to how this caching works since all I've seen are examples for a single feed, rather than multiple feeds. Thanks.

share|improve this question
add comment

1 Answer

The example your using from the codex adds and removes it (probably not something you want to do), and is not very clear.

By default WordPress will cache the feed for 12 hours using wp_feed_cache_transient_lifetime, the actual code WP uses for 12 hours is $lifetime = 43200

If you want to alter the cache time globally for all simplepie feeds, you can add new time to the filter,

//change cache to 24hrs
add_filter( 'wp_feed_cache_transient_lifetime', create_function('$a', 'return 86400;') );

If instead you want specific feeds to have different cache times, you can use the $url parameter in the filter.

add_filter( 'wp_feed_cache_transient_lifetime', 'change_feed_speed' );

function change_feed_speed( $lifetime, $url ) {

    if( 'http://mysite.org/some-feed.rss' == $url )
        return 86400;

    return $lifetime;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.