How to Create Custom RSS Feeds in WordPress

If you purchase through a link on our site, we may earn a commission. Learn more.

With the abundance of news pieces and the number of blog sites continuously growing, keeping track of what’s coming round can be tiring. Fortunately, there is such a standard as RSS that makes it super easy. It provides a simple yet an effective way to keep up with the latest news and what matters to you most. In other words, RSS grabs content from a particular website and aggregates it into a news reader application.
Table of Contents
WP Engine High Performance Hosting
BionicWP Hosting

With the abundance of news pieces and the number of blog sites continuously growing, keeping track of what’s coming round can be tiring. Fortunately, there is such a standard as RSS that makes it super easy. It provides a simple yet an effective way to keep up with the latest news and what matters to you most. In other words, RSS grabs content from a particular website and aggregates it into a news reader application.

When implemented well, this automation solution saves readers time greatly. Really Simple Syndication monitors the websites that you’re interested in and synchronises all their new content updates as they come out. It takes the daunting search routine on its part, leaving you the time just to glimpse and enjoy the latest.  

You can use these feeds in a number of ways. For very basic implementation, WordPress has its built-in default RSS feeds. But there are also more specialized purposes for delivering a more specific type of content, for which you may need to create a custom RSS. In this article, we’ll show you how to do this.

Please note that this tutorial is not targeted for beginner level of WordPress users.

As always, before making any major changes to a live website, remember to complete your WordPress website backup.

The first thing you need to do is initialize your custom RSS action in your theme’s functions.php file.

add_action( 'init', 'myRSS' );
function myRSS() {
  add_feed( 'feedname', 'myRSSFeed' );
}

The function above adds the feed to the template through the add_feed function. The add_feed function has two arguments, feedname, and a callback function. The feedname will make up your new feed URL yourdomain.com/feed/feedname, and the callback function will be called to actually create the feed. So, we need to create the myRSSFeed function in theme’s functions.php file.

You can place the RSS code directly in the callback function. By using get_template_path, we can keep the functionality separate to the layout.

function myRSSFeed() {
  get_template_path( 'slug', 'name' );
}

The get_template_path function takes the slug and name arguments to a template file like wp-content/themes/themename/slug-name.php or wp-content/themes/themename/slug.php.

Take a look on the template file.

<?php
/**
 * RSS Feed Template by Julia N.  
 */
$limitCount = 7; // The posts limit to show
$posts = query_posts('showposts=' . $limitCount);


// Setting up content type and charset headers 
header('Content-Type: '.feed_content_type('rss-http').';charset='.get_option('blog_charset'), true);

// Setting up valid XML encoding
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?> 

<!-- Declaring XML Validators namespaces -->
<rss version="2.0"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
        <?php do_action('rss2_ns'); ?>>
<!-- Declaring channel with articles data --> 
<?php $dateTimeFormat = 'D, M d Y H:i:s'; ?>
<channel>
        <title><?php bloginfo_rss('name'); ?> - Feed</title>
        <link><?php bloginfo_rss('url') ?></link>
        <description><?php bloginfo_rss('description') ?></description>
        <lastBuildDate><?php echo mysql2date($dateTimeFormat, get_lastpostmodified(), false); ?></lastBuildDate>
        <language><?php echo get_option('rss_language'); ?></language>
        <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'daily' ); ?></sy:updatePeriod>
        <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
        <?php do_action('rss2_head'); ?>
        <?php while (have_posts()) : the_post(); ?>
                <item>
                        <title><?php the_title_rss(); ?></title>
                        <link><?php the_permalink_rss(); ?></link>
                        <pubDate><?php echo mysql2date($dateTimeFormat, get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
                        <dc:creator><?php the_author(); ?></dc:creator>
                        <guid isPermaLink="false"><?php the_guid(); ?></guid>
                        <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
                        <content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
                        <?php rss_enclosure(); ?>
                        <?php do_action('rss2_item'); ?>
                </item>
        <?php endwhile; ?>
</channel>
</rss>

The final thing that is left to do is flush your WordPress rewrite rules. To proceed with this action easily, you need to log in to the WordPress admin panel, and go Settings -> Permalinks. Once there, just click Save Changes, this will flush the rewrite rules. Now you can access your feed at yourdomain.com/feed/feedname.

The last thing to mention is that you can aggregate as many RSS feeds from as many sources as you’d like if you apply some ingenuity. The easiest way to do so is to make a use of WP RSS Aggregator. It is a very neat plugin for importing, merging and displaying RSS and Atom feeds on your WordPress site. This solution is very comprehensive and elegant for WordPress, so it does not take much effort and time to get started.

Julia Nimchynska

Marketing manager at CMS2CMS, a travel lover, simplicity enthusiast, a fan of jazz and yoga.

Discover more from our , archives ↓

Popular articles ↓

16 Responses

  1. Anybody can help me to get the total likes in feed
    currently i am getting the total commnet
    i am using the plugin kodes post likes
    which is not generating the total likes in the rss feeed

    thanks

  2. Hi,
    Thanks for your post!
    Any idea how to add a custom post type to this rss feed ?

    Regards

    1. Hey Jivan, if you mean the title of the whole RSS feed, then yes. It’s explained here:

      https://kb.wprssaggregator.com/article/219-how-to-create-custom-rss-feeds

  3. I have tried three dif types of code and they come up without a 404 but the files are blank. Any ideas?

  4. get_template_path( ‘slug’, ‘name’ );

    That is not a function it is

    get_template_part( ‘slug’, ‘name’ );

  5. Great tutorial – would be nice to expand on getting set categories or even Custom post types or attributes / tags

    1. I’m glad you like it, Scott. Thanks for your recommendations, I’ll consider them in my further posts 🙂

  6. Good tutorial (y)
    but, i prefer to use WP RSS Aggregator because its very easy and simple

    regards

Share Your Thoughts

Your email address will not be published. Required fields are marked *

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.