Custom RSS feeds in WordPress

June 09th, 2009 – 22 Comments

I use FeedBurner to track all my feed subscribers, but sometimes I want to provide a different feed to other people (without my feed signature for instance, or using excerpts instead of full feeds).

Now when you use FeedBurner’s Feedsmith plugin, this’ll redirect all of your normal feeds to FeedBurner, and that’s a smart thing to do: you don’t want to give those custom feeds to people they aren’t meant for.

There’s an easy solution though, just follow these steps:

  1. create a custom page template within your theme that serves out the feed (code below)
  2. create a new page (which can be blank), with a secret URL
  3. use your newly created “Custom Feed” template for that page
  4. open the secret URL in your browser and voila: your custom feed is ready

The template should look something like this:

Template Name: Custom Feed

$numposts = 5;

function yoast_rss_date( $timestamp = null ) {
  $timestamp = ($timestamp==null) ? time() : $timestamp;
  echo date(DATE_RSS, $timestamp);

function yoast_rss_text_limit($string, $length, $replacer = '...') { 
  $string = strip_tags($string);
  if(strlen($string) > $length) 
    return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;   
  return $string; 

$posts = query_posts('showposts='.$numposts);

$lastpost = $numposts - 1;

header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?><rss version="2.0">
  <title>Yoast E-mail Update</title>
  <description>The latest blog posts from</description>
  <pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
  <lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
<?php foreach ($posts as $post) { ?>
    <title><?php echo get_the_title($post->ID); ?></title>
    <link><?php echo get_permalink($post->ID); ?></link>
    <description><?php echo '<![CDATA['.yoast_rss_text_limit($post->post_content, 500).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    <pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
    <guid><?php echo get_permalink($post->ID); ?></guid>
<?php } ?>

22 Responses to Custom RSS feeds in WordPress

  1. Techadictos
    By Techadictos on 28 November, 2009

    is any wordpress plugin limit words in the /feed ? because feedburner send by emails all of post
    not a excerpt

  2. kinta mahadji
    By kinta mahadji on 28 November, 2009

    Dear sir, I have try the script but why when I access the page there no rss content in it ? (*sorry for my bad english)

  3. sexy costumes
    By sexy costumes on 26 October, 2009

    Thank you very much very nice article
    Great information! Very useful for me. Thanks a lot.

  4. Antoine
    By Antoine on 8 October, 2009

    I found a solution to add a automatic thumbail to my RSS feed to send a daily newsletter via MailShimp. Here the code of my RSS feed: (2d message, even you don’t speak french, you’ll understand my code !)

  5. Antoine
    By Antoine on 1 October, 2009

    Thanks a lot!
    One suggestion: you can use parameter to create filtered RSS feeds (tags, categories, authors) using templates tags
    One question: how would feed the entire post with images and HTML layout (bold, etc.)

  6. Ruud Kok
    By Ruud Kok on 25 August, 2009

    Nice! Very useful indeed, just implemented it for one of our blogs :)

  7. Richard
    By Richard on 27 June, 2009

    Hi – Thanks very much for this. Really helpful. I’m wondering if you could help me please with something. It seems possible to restrict the custom feed to a single category and to make a call to a custom field (“events date”) of those posts, in a way which would replace the ordinary “pub date” with, in fact, the custom field date.

    Do you think this is doable? I’d gladly donate if you were able to help me out with this.

    Thanks in advance for any assistance you can give


  8. Otto
    By Otto on 17 June, 2009

    There is a somewhat nicer plugin-based way to do this sort of thing.

    function my_feed() {
    // code to output the feed goes here
    add_feed(‘feedname’, ‘my_feed’);

    Now you can call that feed up on your site using That’s all there is to it.

  9. fikret
    By fikret on 17 June, 2009

    could anyone say wat makes this one different from default RSS feeds? :S

  10. Stevo
    By Stevo on 16 June, 2009

    Great stuff. I’ve been looking to create a custom feed. Thanks!

  11. 123 Tweak
    By 123 Tweak on 16 June, 2009

    Great tips

  12. Matthew
    By Matthew on 15 June, 2009

    Is there a way to limit the feed to ONLY showing 100 words, not characters but words?

    I would love to be able to do that.

  13. IJ Styles
    By IJ Styles on 13 June, 2009

    I’ve been searching for this info lately and finally found it. Haha.. Thanks for this useful info, I can now customize my own feed.

  14. Ernie Smith
    By Ernie Smith on 12 June, 2009

    Hey, dig this post a bunch, but out of curiosity, how would your recommend best handling a RSS feed that uses a lot of custom fields? I have a solution that works (mostly) based on a hacked WordPress plugin due to the fact that RSS support for custom fields isn’t right out of the box for RSS feeds, but it’s not perfect and once in a while the fields show up in the wrong order:

    This method would be very useful for me because I could split the feeds up by sections or have a feed specifically for posts I’d like to feature on Twitter or Facebook.

    If you have any thoughts, I’d love to hear them.

  15. Dr. Md. Masud Kabir
    By Dr. Md. Masud Kabir on 11 June, 2009

    My website is Doctors View. How Can I create feedburner account and how can I place it to my WP blog? please help me.

  16. Kaushik
    By Kaushik on 10 June, 2009

    Please bear with me…I have some questions that may seem silly to you…
    1. Can I use this as a newsletter? I want something to go out to feedburner subscribers but don’t want to post it.
    2. “open the secret URL in your browser and voila: your custom feed is ready”
    What does that mean? When I open the URL, who does the feed go to? Does this mean whatever I post in this template will go the feedburner subscribers?

  17. Jasja ter Horst
    By Jasja ter Horst on 10 June, 2009

    Thanx for this feed / page template, very useful for producing custom feeds.

  18. FreewareMatter
    By FreewareMatter on 9 June, 2009

    Cool! This is a creative trick. With this method, we can offer custom feed for categories, tags, … Thank you very much!

Check out our must read articles about Analytics