Feedsby Internet Solutions

How to Create a Custom RSS Feed in WordPress, Step by Step

15 tháng 8, 20268 phút đọcNguồn cấp RSS
How to Create a Custom RSS Feed in WordPress, Step by Step

Short answer: Before writing code, check whether a built-in feed URL already does the job: WordPress offers feeds for every category, tag, author, custom post type and search, and query parameters can combine them. If you need a different structure, such as custom fields, a partner’s required format or a mix of post types, register a new feed with add_feed(), give it its own template, flush the rewrite rules once, and validate the result. Keep the code in a small plugin, not in the theme.

Custom feeds solve real problems: a partner portal that wants only events with a start date, a job board that needs location and salary fields, a mobile app that expects a particular set of tags, or an internal dashboard that should show several post types together. WordPress makes custom feeds surprisingly easy, but it is just as easy to create one that breaks on the next update or slows down the site. This guide covers when to build one, how to do it properly and how to keep it healthy.

First, check whether you need a custom feed at all

A lot of “custom feed” requests can be solved with URLs WordPress already understands. Before writing code, try these:

If one of these delivers the right items and the partner accepts standard RSS, you are done. Build a custom feed only when you need different fields, a different format, or logic that URLs cannot express.

Common reasons for a truly custom feed

How WordPress feeds work under the hood

Understanding the mechanism helps you avoid mistakes. When someone requests /feed/, WordPress:

  1. Matches the URL against rewrite rules and sets the feed query variable.
  2. Runs the normal main query for that context, such as the latest posts or a category archive, limited to the number set in Settings → Reading.
  3. Calls the handler registered for the feed type, which loads a template such as feed-rss2.php from WordPress core.
  4. The template loops through the query results and prints the XML, firing hooks like rss2_ns, rss2_head and rss2_item along the way.

This gives you three levels of customisation: change the query, hook into the existing template to add elements, or register a completely new feed with its own template.

Level 1: adjust the query of existing feeds

To change which items appear in a feed without changing its format, use the pre_get_posts action and check is_feed(). For example, to include a “podcast” custom post type in the main feed:

add_action( 'pre_get_posts', function ( $q ) {
    if ( $q->is_main_query() && $q->is_feed() && ! $q->is_category() ) {
        $q->set( 'post_type', array( 'post', 'podcast' ) );
    }
} );

Be careful: this changes the feed for every subscriber. Test it on staging and make sure the added post type does not flood readers with old items, since readers consider anything with a new identifier as new.

Level 2: add elements to the existing feed

When you only need extra fields, hook into the RSS 2.0 template. Declare any namespace with rss2_ns and output elements per item with rss2_item:

add_action( 'rss2_item', function () {
    $start = get_post_meta( get_the_ID(), 'event_start', true );
    if ( $start ) {
        echo '<ev:startdate>' . esc_html( $start ) . '</ev:startdate>' . "\n";
    }
} );

Remember that every prefixed element needs its namespace declared on the root element, or the feed becomes invalid. Use established namespaces when they exist rather than inventing your own, and escape every value.

Level 3: register a completely new feed

For a feed with its own URL and structure, use add_feed(). It registers a feed name and a callback; WordPress then serves it at /feed/name/ and /?feed=name.

add_action( 'init', function () {
    add_feed( 'partners', 'render_partner_feed' );
} );

function render_partner_feed() {
    $posts = get_posts( array(
        'post_type'      => array( 'post', 'event' ),
        'posts_per_page' => 30,
        'category_name'  => 'partners',
    ) );
    header( 'Content-Type: application/rss+xml; charset=UTF-8' );
    // Load your template file here and loop through $posts.
    require __DIR__ . '/templates/feed-partners.php';
}

In the template, output a normal RSS 2.0 structure: the XML declaration, the rss root with namespaces, a channel with title, link and description, and one item per post with title, link, guid, pubDate and description. Use get_the_guid() or the permalink for guids, mysql2date( DATE_RSS, ... ) for dates, and esc_html() or esc_url() for every value.

After adding a new feed, flush the rewrite rules once: visit Settings → Permalinks and click Save, or call flush_rewrite_rules() on plugin activation. Never flush on every page load; it is slow.

Agree on a specification before you build

When a custom feed is meant for a partner, an app developer or another team, most of the effort goes into agreeing what the feed should contain, not into the code. A short written specification saves several rounds of fixes. It should cover:

Send the partner a sample feed early, even with test content, and ask them to import it into their system. Problems with element names or date formats show up in minutes that way, rather than after launch. Keep the specification with the plugin code so that future changes do not break the agreement by accident.

Performance, caching and security

Testing a custom feed

  1. Open the feed URL and view the source; confirm nothing appears before the XML declaration.
  2. Run it through the W3C Feed Validation Service and fix every error.
  3. Check it in a real reader and, if it is for a partner, in their system or with their test tool.
  4. Publish a new test item and confirm it appears after caches expire.
  5. Re-test after WordPress, theme and plugin updates.

If what you need is not a new format but a combination of feeds, for example your WordPress feed plus a partner’s news page that has no RSS, filtered by keywords, a custom WordPress feed is the wrong tool. Our tool, Feeds, merges several sources into one RSS link, removes duplicates and keeps only items with your words, without any code on your site. See the plans.

Related reading

The bottom line

Most custom feed needs in WordPress can be met with built-in feed URLs and query parameters. When you need different fields or a different structure, adjust the query with pre_get_posts, add elements with the rss2_item hook, or register a new feed with add_feed() and your own template. Keep the code in a plugin, cache the output, escape every value, and validate before handing the URL to anyone.

FAQ

How do I create a custom RSS feed in WordPress?

Register it with add_feed() in a small plugin, point it to a callback that runs your query and loads an RSS template, then flush rewrite rules once by saving the permalink settings. The feed becomes available at /feed/your-name/.

Can I create a feed for a custom post type without code?

Usually yes. Use /?post_type=your_type&feed=rss2, or the post type archive address followed by /feed/ if the post type has an archive.

How do I add custom fields to my WordPress RSS feed?

Hook into rss2_item to output extra elements for each item, and declare any namespace with rss2_ns. Escape every value and validate the feed afterwards.

Why does my new custom feed return a 404?

The rewrite rules have not been flushed since the feed was registered. Visit Settings → Permalinks and click Save Changes once, then try again.

Should the code go in functions.php?

It is better in a small custom plugin. Code in the theme disappears when the theme changes, which silently breaks any partner or tool that uses the feed.

#CMS#Publishing#RSS#WordPress
Tạo nguồn cấp đầu tiên của bạn — miễn phí.Nguồn cấp từ mọi trang. Mọi sản phẩm trong mọi danh mục.
Bắt đầu miễn phí
Internet Solutions

Sản phẩm khác từ đội ngũ chúng tôi

Do Internet Solutions phát triển. Hãy thử các sản phẩm khác của chúng tôi — mỗi sản phẩm giúp bạn tiết kiệm thời gian theo một cách riêng.

internet-solutions.net ↗
Tự động đăng mạng xã hộiĐang hoạt động
PostRSS

Bài mới từ nguồn cấp RSS của bạn được tự động đăng lên Facebook, X, LinkedIn, Telegram và hơn 60 mạng khác.

Gói miễn phí · từ 2014Truy cập →
Chat trực tuyến AI cho websiteĐang hoạt động
Talkmio

Website của bạn trả lời khách truy cập 24/7 từ chính nội dung của bạn, bằng ngôn ngữ của họ.

Gói miễn phí · không cần thẻTruy cập →
Trợ lý AIĐang hoạt động
Ask Mio

Trò chuyện, viết code, thiết kế, viết bài và nghiên cứu. Mio chọn mô hình tốt nhất cho từng việc.

Gói miễn phíTruy cập →
Lái tự động AI cho blog và mạng xã hộiĐang hoạt động
AI Blog Autopilot

AI viết bài SEO dài 2.000–3.000 từ và chia sẻ từng bài lên hơn 58 mạng xã hội.

3 bài đầu tiên miễn phíTruy cập →
Kiểm tra sức khỏe websiteĐang hoạt động
Site AI Audit

SEO, tốc độ, SSL, bảo mật và cấu hình email trong một báo cáo, sắp xếp theo việc cần sửa trước.

Lần kiểm tra đầu tiên miễn phíTruy cập →
Thu thập SEO chuyên sâuĐang hoạt động
Site SEO AI Audit

Thu thập SEO toàn diện trên 7 lĩnh vực, gồm cả khả năng hiển thị trong tìm kiếm AI, với cách sửa xếp theo mức tác động.

Lần kiểm tra đầu tiên miễn phíTruy cập →
Phát triển website và SEOĐang hoạt động
Internet Solutions

Website, cửa hàng trực tuyến và hệ thống theo yêu cầu, do đội ngũ của chúng tôi thiết kế, xây dựng và vận hành.

Từ 2011Truy cập →
Feeds
Tổng quan quyền riêng tư

Website này dùng cookie để mang lại trải nghiệm người dùng tốt nhất có thể. Thông tin cookie được lưu trong trình duyệt của bạn và thực hiện các chức năng như nhận ra bạn khi bạn quay lại, giúp đội ngũ chúng tôi hiểu phần nào của website bạn thấy thú vị và hữu ích nhất.