keepcalculator

06 Numbers and Powers

Custom RSS XML WordPress: How to Build One

Card No. 06·71161 · Updated · Standard formula, shown below

To create a custom RSS XML feed in WordPress, you either use a plugin like WP RSS Aggregator or Custom Feed, or you write a custom PHP function that hooks into WordPress’s feed system. This article explains both approaches, what a custom feed can do for you, and the common pitfalls to avoid.

What a Custom RSS Feed Is

WordPress generates RSS 2.0, Atom, and RDF feeds automatically at URLs like /feed/ and /feed/atom/. A custom feed is any feed that contains different content, a different structure, or is filtered by criteria the default feeds don’t support. For example, you might want a feed that only shows posts from a specific category, excludes certain tags, includes custom post types, or outputs extra metadata like product prices or event dates.

The default WordPress feed is fine for most blogs, but if you’re building a podcast, a price comparison site, or a news aggregator, you’ll need a feed that’s tailored to your exact data. Custom feeds are also used to provide data to third-party services that require a specific XML schema, such as Google Merchant Center or Apple Podcasts.

Why You Might Need a Custom Feed

There are several common reasons to build a custom RSS feed:

  • Custom post types: By default, WordPress only includes standard posts in the main feed. If you have a custom post type like “products” or “events,” you need a custom feed to expose them.
  • Filtered content: You may want to exclude certain categories, authors, or posts with a specific tag from your feed.
  • Extra metadata: Podcast feeds need itunes:duration and itunes:image tags. Product feeds need g:price and g:availability.
  • Performance: If your default feed is slow because it queries all posts, a custom feed can be optimized to run faster.

How WordPress Feeds Work Under the Hood

WordPress handles feed requests through the do_feed action. When someone visits /feed/, WordPress loads the feed-rss2.php template from the wp-includes directory. That template runs a standard query for the latest posts and outputs them in RSS 2.0 format.

To create a custom feed, you have two main paths:

  1. Add a new feed endpoint using add_feed(). This lets you create a URL like /feed/custom that uses a custom template.
  2. Override the default feed template by placing a file named feed-rss2.php in your theme directory. This changes the output of the default /feed/ URL.

Both methods require you to write PHP code that outputs XML. The add_feed() approach is cleaner because it doesn’t interfere with the default feed.

Method 1: Using a Plugin (No Code)

If you don’t want to write code, several plugins can create custom feeds. The most popular are:

  • WP RSS Aggregator – primarily for importing feeds, but it also has a feature to create custom feed endpoints.
  • Custom Feed for Google Merchant – specifically for WooCommerce product feeds.
  • Feedzy RSS Feeds – allows you to create custom feeds with templates.
  • Simple Custom RSS – a lightweight plugin that lets you select which post types and taxonomies to include.

Plugins are the fastest solution, but they often add overhead and may not give you full control over the XML structure. If you need a very specific output, custom code is better.

Method 2: Custom Code with add_feed()

Here’s a step-by-step example of how to create a custom feed that only shows posts from a specific category. This code goes in your theme’s functions.php file or a site-specific plugin.

  1. Add the feed endpoint:
    add_action('init', 'my_custom_feed');
    function my_custom_feed() {
        add_feed('custom-category', 'my_custom_feed_callback');
    }
  2. Define the callback function that outputs the feed:
    function my_custom_feed_callback() {
        // Query posts from category ID 5
        $args = array(
            'cat' => 5,
            'posts_per_page' => 10,
        );
        $query = new WP_Query($args);
        
        // Output the XML header
        header('Content-Type: application/rss+xml; charset=' . get_option('blog_charset'));
        echo '<?xml version="1.0" encoding="' . get_option('blog_charset') . '"?>';
        ?>
        <rss version="2.0">
        <channel>
            <title><?php bloginfo_rss('name'); ?> - Custom Feed</title>
            <link><?php bloginfo_rss('url'); ?></link>
            <description><?php bloginfo_rss('description'); ?></description>
            <?php while($query->have_posts()) : $query->the_post(); ?>
                <item>
                    <title><?php the_title_rss(); ?></title>
                    <link><?php the_permalink_rss(); ?></link>
                    <pubDate><?php echo get_post_time('r', true); ?></pubDate>
                    <description><?php the_excerpt_rss(); ?></description>
                </item>
            <?php endwhile; ?>
        </channel>
        </rss>
        <?php
    }
  3. After adding the code, go to Settings → Permalinks and click “Save Changes” to flush rewrite rules. Your custom feed will be available at /feed/custom-category.

This is a minimal example. In practice, you’ll want to include more elements like <guid>, <author>, and possibly custom namespaces.

Common Problems and How to Fix Them

Here are the most frequent issues people encounter when building custom feeds:

  • 404 error on the feed URL: This usually means you didn’t flush permalinks. Go to Settings → Permalinks and click “Save Changes”. If you’re using a caching plugin, clear the cache as well.
  • Feed shows blank page: This is often a PHP error. Enable WP_DEBUG to see the error message. Make sure there are no spaces or characters before the <?php opening tag in your callback.
  • Feed not updating: Some caching plugins cache feeds aggressively. Exclude your custom feed URL from caching, or set a shorter cache lifetime.
  • Invalid XML: If your feed has unescaped ampersands or other special characters, it will break. Use functions like esc_html() and the_title_rss() which handle escaping automatically.
  • Slow feed: If your custom feed runs heavy queries, consider adding caching with transients or using a plugin like WP Super Cache.

Comparing Custom Feeds to Default Feeds

The default WordPress feed is simple and works out of the box. It includes the 10 most recent posts, with title, link, description, and author. Custom feeds give you control over:

  • Number of items: Default is 10; custom can be anything.
  • Order: Sort by date, title, or custom meta.
  • Content: Include full content, excerpts, or custom fields.
  • Namespaces: Add iTunes, Google, or custom namespaces.

Custom feeds are more work but are essential for specific use cases like podcasting, e-commerce, and data syndication.

Frequently Asked Questions

How do I add a custom feed without a plugin?

You can add a custom feed by placing the add_feed() code in your theme’s functions.php file. Then define a callback function that outputs the XML. Remember to flush permalinks after adding the code.

Can I create a feed for a custom post type?

Yes. In your custom feed query, set 'post_type' => 'your_post_type'. For example, if you have a ‘book’ post type, you can create a feed that only outputs books.

Why is my custom feed not validating?

Validation errors are usually caused by unescaped characters, missing required elements like <guid>, or incorrect XML structure. Use the W3C Feed Validation Service to see specific errors. Make sure you’re using the correct WordPress functions for outputting content.

How do I exclude certain categories from my feed?

In your custom feed query, you can use the cat parameter with a negative value to exclude a category. For example, 'cat' => '-5' excludes category ID 5. You can also use 'category__not_in' with an array of IDs.

Do custom feeds affect site performance?

They can, if the query is heavy or if many users access the feed. To mitigate, add caching to your feed callback using WordPress transients, or use a plugin that caches feeds. Also, limit the number of posts returned to what you actually need.

Can I use a custom feed for podcasting?

Yes. Podcast feeds require specific iTunes tags like itunes:duration and itunes:image. You can add these by defining the iTunes namespace in your feed and outputting the tags for each episode. This is a common use case for custom feeds.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →