WP Munk > Blog > Code Snippets > How to Fetch and Show Posts from a RSS Feed in WordPress

How to Fetch and Show Posts from a RSS Feed in WordPress

RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it.

If you would like to use RSS feed of some other site to show their latest posts into your site/blog then WordPress has a handy function which you can use to pull the posts from another site.

Following code helps you fetch posts from an external RSS feed to display on your site.

    <?php // Get RSS Feed(s) include_once( ABSPATH . WPINC . '/feed.php' ); // Get a SimplePie feed object from the specified feed source. $rss = fetch_feed( 'http://wphelpguide.com/feed/' ); // enter your rss feed url here $maxitems = 3; if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly // Figure out how many total items there are, but limit it to 5. $maxitems = $rss->get_item_quantity( 3 ); 
    
        // Build an array of all the items, starting with element 0 (first element).
        $rss_items = $rss->get_items( 0, $maxitems );
    
    endif;
    ?>

In above code, you first include the WordPress Core file called feed.php which enables the use of fetch_feed function to be used in your file. Then you enter the feed URL in the next line and then define the number of posts you would like to fetch. I would suggest to enter number not more than 10 as fetching large number of posts can slow down your site. After that the function checks the fetched quantity and makes sure function has pulled required number of posts from the RSS feed.

After that posts following code to show the fetched RSS feed output

    

<ul>
        <?php if ( $maxitems == 0 ) : ?>
            

<li><?php _e( 'No items', 'my-text-domain' ); ?></li>


        <?php else : ?>
            <?php // Loop through each feed item and display each item as a hyperlink. ?>
            <?php foreach ( $rss_items as $item ) : ?>        
                

<li>
                    <a target="_blank" href="<?php echo esc_url( $item->get_permalink() ); ?>"
                        title="<?php printf( __( 'Posted {37256cf0ee31db86add5b6eba3983cfb5fab0b039501789ef81a53d1a467d5f8}s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                        <?php echo $item->get_content(); ?>


<div style="clear:both;"></div>


                        <?php echo esc_html( $item->get_title() ); ?>
                    </a>
                </li>


            <?php endforeach; ?>
        <?php endif; ?>
    </ul>


This code first checks whether or not the posts are pulled and if correct number of posts are pulled then it shows the output. You can customize the output as per your requirement and include and exclude the elements which you want to show.

By default WordPress caches the RSS feed output for 12 hours, which means even if the external site has updated their RSS Feed, WordPress won’t show newer posts until 12 hours have passed. To change that value you can use another filter called wp_feed_cache_transient_lifetime to customize the default time. Paste following code into your theme’s functions.php file and edit the seconds numbers as per your requirement.

function return_cache_time( $seconds ) {
// change the default feed cache recreation period to 2 hours
return (int) 600;  time is given in seconds.. default is 43200
}
 
//set feed cache duration
add_filter( 'wp_feed_cache_transient_lifetime', 'return_cache_time');

About The Author


Mohammad Tajim

Hello, I am Mohammad Tajim a WordPress Developer with over 14 years of experience building WP Products. I made Munk WordPress Theme and other themes available at metricthemes.com. Follow me on twitter @tajim