Combining multiple RSS feeds using fetch_feed

I’m using fetch_feed to display an rss feed on a theme i’m working on.

I think the documentation implies that you can combine multiple feeds, can anyone confirm this, ideally with an example.

Read More

Thanks in advance!
George

Related posts

Leave a Reply

1 comment

  1. You can technically pass multiple feed URLs to fetch_feed() as an array, and it’ll grab them all at once. But the return is a total mess, and I couldn’t personally figure out how to parse it.

    If nobody else knows how to do this with fetch_feed(), I can offer a solution using the SimplePie class (which fetch_feed actually uses anyway). The SimplePie class has some helper methods for parsing the return, and they make things pretty easy.

    Try something like:

    // Contains the SimplePie class
    require_once (ABSPATH . WPINC . '/class-feed.php');
    
    // New class instance
    $feed = new SimplePie();
    
    // You feed URLs
    $feed->set_feed_url(array('http://rss.cnn.com/rss/cnn_topstories.rss', 'http://cuteoverload.com/feed/'));
    
    // Do it!
    $feed->init();
    $feed->handle_content_type();
    
    // Loop the results
    foreach($feed->get_items() as $item) {
    
        echo $item->get_title();
        echo '<hr/>';
    
    }
    

    Additional SimplePie methods include get_permalink() and get_description().

    The only downside to this approach is that is SimplePie is ever phased out of WordPress in favor of another class, this’ll break.

    UPDATE

    As @Rarst pointed out in the comments, you don’t need to access SimplePie directly. You can use its methods on the object that fetch_feed() returns. So the answer is much simpler than I thought:

    $feed = fetch_feed(array('http://rss.cnn.com/rss/cnn_topstories.rss', 'http://cuteoverload.com/feed/'));
    
    // Loop the results
    foreach($feed->get_items() as $item) {
    
        echo $item->get_title();
        echo '<hr/>';
    
    }