WordPress: How To Publish Feeds For Each Category On Your Blog

By default, a WordPress blog has a feed that incorporates all its posts, regardless of category. One way to improve personalization and segmentation for your site visitors is to enable an RSS feed specific to their categories of interest. You can also utilize a category-specific feed to publish an email newsletter. However, you can create custom category feeds for your WordPress blog or custom post types if you’d like.

WordPress Category Feeds

Here’s code that you can add to your child theme’s functions.php file that generates category-specific RSS feeds in WordPress with both inclusion and exclusion lists for category IDs:

function custom_category_feeds() {
    $categories = get_categories();

    // Define an array of category IDs to include and exclude
    $included_category_ids = array(3, 4); // Add IDs of categories to include
    $excluded_category_ids = array(1, 2); // Add IDs of categories to exclude

    foreach ($categories as $category) {
        $category_id = $category->term_id;

        // Check if the category should be excluded
        if (in_array($category_id, $excluded_category_ids)) {
            continue; // Skip excluded categories
        }

        // Check if the category should be included
        if (!empty($included_category_ids) && !in_array($category_id, $included_category_ids)) {
            continue; // Skip categories not in the inclusion list
        }

        $category_slug = $category->slug;
        $category_name = $category->name;

        // Start building the RSS feed content
        $rss_feed = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
        $rss_feed .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' . "\n";
        $rss_feed .= '<channel>' . "\n";
        $rss_feed .= '<title>' . $category_name . ' RSS Feed</title>' . "\n";
        $rss_feed .= '<link>' . get_bloginfo('url') . '</link>' . "\n";
        $rss_feed .= '<description>' . $category_name . ' RSS Feed</description>' . "\n";
        $rss_feed .= '<atom:link href="' . esc_url(site_url("/category/$category_slug/feed/")) . '" rel="self" type="application/rss+xml" />' . "\n";

        // Query posts in the current category
        $args = array(
            'cat' => $category_id,
            'posts_per_page' => 10, // Adjust as needed
        );
        $category_posts = new WP_Query($args);

        while ($category_posts->have_posts()) {
            $category_posts->the_post();
            $rss_feed .= '<item>' . "\n";
            $rss_feed .= '<title>' . get_the_title() . '</title>' . "\n";
            $rss_feed .= '<link>' . get_permalink() . '</link>' . "\n";
            $rss_feed .= '<pubDate>' . get_the_time('D, d M Y H:i:s O') . '</pubDate>' . "\n";
            $rss_feed .= '</item>' . "\n";
        }

        wp_reset_postdata();

        $rss_feed .= '</channel>' . "\n";
        $rss_feed .= '</rss>';

        // Output the feed
        header('Content-Type: application/rss+xml; charset=UTF-8');
        echo $rss_feed;
    }
}

add_action('do_feed_category', 'custom_category_feeds', 10, 1);
add_action('do_feed_category_rss2', 'custom_category_feeds', 10, 1);

Here’s an explanation of the code:

    The key feature of this code is the ability to specify both an inclusion list and an exclusion list of category IDs, giving you fine-grained control over which categories are included or excluded in the generated category-specific RSS feeds.

    Your WordPress Category Feed

    Users can use the URL structure provided earlier to access the custom category-specific feed you’ve created in WordPress. The URL format to access a category-specific feed is as follows:

    http://yourwebsite.com/category/{category-name}/feed/

    Here’s a breakdown of how to call the feed:

    1. Replace yourwebsite.com with your actual website domain or URL.
    2. Replace {category-name} with the slug of the category for which you want to access the feed. The slug is a lowercase, hyphen-separated version of the category name. For example, if your category name is Marketing Tips, the slug could be marketing-tips.
    3. Add /feed/ to the end of the URL. This indicates that you want to access the RSS or Atom feed for the specific category.

    For example, if your website is “example.com,” and you want to access the feed for the “Marketing Tips” category, the URL would be:

    http://example.com/category/marketing-tips/feed/

    Users can enter this URL into their web browser or use feed reader applications to subscribe to the category-specific feed. This URL will provide them with the RSS or Atom feed for the selected category, making it easy for them to stay updated on the content in that category.

    Exit mobile version