The fetch_feed() WordPress PHP function builds a SimplePie object based on an RSS or Atom feed from a provided URL.
Usage
To use the fetch_feed() function, you simply need to pass the URL of the RSS or Atom feed you want to retrieve. If you pass an array of URLs, the feeds are merged using SimplePie’s multifeed feature.
$rss = fetch_feed('http://example.com/rss/feed/goes/here');
Parameters
- $url (string|array) – Required. The URL of the feed to retrieve. If an array of URLs is provided, the feeds are merged using SimplePie’s multifeed feature.
More Information
See WordPress Developer Resources: fetch_feed()
Examples
Display the Title of the Most Recent Post from a Feed
// Fetch the feed $rss = fetch_feed('http://example.com/rss/feed/goes/here'); // Check for errors in the feed if (!is_wp_error($rss)) { // Get the first item from the feed $item = $rss->get_item(0); // Display the title of the item echo $item->get_title(); }
This example fetches an RSS feed and displays the title of the most recent post.
Display the Titles of the Five Most Recent Posts from a Feed
// Fetch the feed $rss = fetch_feed('http://example.com/rss/feed/goes/here'); // Check for errors in the feed if (!is_wp_error($rss)) { // Get the five most recent items $items = $rss->get_items(0, 5); // Loop through each item and display the title foreach ($items as $item) { echo $item->get_title() . '<br>'; } }
This example fetches an RSS feed and displays the titles of the five most recent posts.
Display a List of Links for an Existing RSS Feed
// Fetch the feed $rss = fetch_feed('http://example.com/rss/feed/goes/here'); // Check for errors in the feed if (!is_wp_error($rss)) { // Get the five most recent items $items = $rss->get_items(0, 5); // Start the list echo '<ul>'; // Loop through each item and display a link foreach ($items as $item) { echo '<li><a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a></li>'; } // End the list echo '</ul>'; }
This example fetches an RSS feed and displays a list of links to the five most recent posts.
Fetch Multiple Feeds and Merge Them
// Fetch multiple feeds $rss = fetch_feed(array('http://example.com/rss/feed/goes/here', 'http://anotherexample.com/rss/feed/goes/here')); // Check for errors in the feed if (!is_wp_error($rss)) { // Get the five most recent items from the merged feeds $items = $rss->get_items(0, 5); // Loop through each item and display the title foreach ($items as $item) { echo $item->get_title() . '<br>'; } }
This example fetches two RSS feeds, merges them together, and displays the titles of the five most recent posts