Create custom RSS feeds in WordPress

Create a customized rss feed in wordpressA standard WordPress installation typically features RSS feeds for latest posts and latest comments. However there can be situations where you need to create additional feeds, based on different criteria. You might want to create a feed that only contains content from one category, or only includes posts from the current week or months.

In this post we’ll demonstrate how easy it is to build customized feeds in WordPress.

The basic feed template

First step is to copy the code below into a new file, and save it in your active theme folder as “custom-rss-feed.php”. This code is a template that extracts posts from WordPress and outputs the content as a RSS 2.0 feed.

Template Name: Custom RSS Feed

$numposts = '1'; // number of posts displayed
$length = '600';  // how many characters do you wish to include per post;

function rss_text_limit($string, $length) {
  $string = strip_tags($string);
  $slength = strlen($string);
  if ($dlength >= $dch) {
    $dfoo1 = (substr("$string", 0, $length));
    $foo = "$dfoo1...";
    return $foo;

$posts = query_posts('showposts='.$numposts.'&orderby=rand');

header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
<rss version="2.0"
 <?php do_action('rss2_ns'); ?>
 <title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
 <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
 <link><?php bloginfo_rss('url') ?></link>
 <description><?php bloginfo_rss("description") ?></description>
 <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
 <?php the_generator( 'rss2' ); ?>
 <language><?php echo get_option('rss_language'); ?></language>
 <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
 <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>

<?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['.rss_text_limit($post->post_content, $length).'<br/><br/>Read more here: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
   <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
   <guid><?php echo get_permalink($post->ID); ?></guid>
<?php } ?>

The code has the following features:

  1. Include the number of posts defined in line 6
  2. Include the first 600 character per post as defined in line 7
  3. Select the posts based on the query in line 19. In this case random posts

We’ll get back to customization of the script later.

When the script is saved in your active theme folder, you should:

  1. Log in to your WordPress admin area
  2. From the dashboard, select “Pages->Add new”.
  3. Give the new page the title “Custom RSS”
  4. Select “Custom RSS Feed” as page template (see image below)
  5. Publish the page without adding any content to the page

create custom rss feed in wordpress

Point your browser to “http://<your blog url>/custom-rss”, and now you can see the new customized rss feed based on the code above.

See example here

Adjusting the feed

As mentioned before the current template creates a feed with one or more random posts. However the script can easily be adjusted to select posts based on other criteria.

To display posts for a specific category, defined by category name, change the selection criteria in line XX to:

query_posts('category_name=<category name>');

To display posts in one or more categories, defined by category id’s, change the selection criteria in line XX to:

query_posts('cat=<category id(s) separated by commas>');

To display posts that has a specific tag, defined by tag name, change the selection criteria in line XX to:

query_posts('tag=<tag name>');

To display posts from a specific author, defined by author id, change the selection criteria in line XX to:


To display posts from the current week, change the selection criteria in line XX to:

$week = date('W');
$year = date('Y');
query_posts('year=' . $year .'&w=' .$week );

There are several other ways you can customize the selection. You can get more inspiration here

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

10 Replies to Create custom RSS feeds in WordPress

  1. Lars says:

    Bin dabei

  2. Joe says:

    Great information – thanks! One additional question…how do you go about changing the Title in the feeds. Currently in my iGoogle the feed is showing as ‘Undefined’ when it should say the name of the blog.

    Thanks again for your help!

  3. jhnidk says:

    You should check if the part of the feed is populated with data. Normally this is used for feed title in iGoogle.

  4. Joe says:

    Hey thanks for your response. The data is feeding out because I can see the content but it seems that the only thing missing is the title within the feed. I didn’t see an option within the WordPress GUI anywhere so I figured it was within one of the php files.

  5. KF says:

    This is great. How would I include enclosure information for each post? I can’t seem to work it out with my limited knowledge.

  6. Ian Douglas says:

    I get “Parse error: syntax error, unexpected ‘&’ in /home/username/public_html/wp-content/themes/whatever/custom-rss-feed.php on line 47”

  7. Job says:

    Thanks for this useful tip ! Is there a way to display full post rather than excerpts ? Thank you.

  8. CE says:

    Genius! Thanks so much. If anyone else gets a Parse error like Ian Douglas, above – you just have to look for the   in lines 7 and 47 and delete them. I think they might get put in there when you use the copy code widget. Then it should work like a charm.

  9. CE says:

    I meant remove the “& nbsp;” in lines 7 and 47. The comments read them as a space like HTML :-)

Trackbacks for this post

  1. links for 2010-06-18Folks Pants | Folks Pants

Comments are now closed for this article.