How to Generate RSS Feeds for Custom Post Types

Custom Post Types were one of the important innovations in WordPress 3.0. They really launched WordPress on the main stage when it comes to usage as a Content Management System (CMS). WordPress is very intuitive in the area of RSS, however it still does not generate automatic feeds for custom post types. So how can we add Custom Post Types to our main WordPress RSS feeds?
Table of Contents

If you purchase through a link on our site, we may earn a commission.

Custom Post Types were one of the important innovations in WordPress 3.0. They really launched WordPress on the main stage when it comes to usage as a Content Management System (CMS). WordPress is very intuitive in the area of RSS, and it also can be queried to generate RSS feeds for Custom Post Types, although it does not provide them automatically as it does for Posts and Comments.

How to Find the RSS Feeds for Custom Post Types

As I referred to, WordPress can be queried to generate RSS feeds for Custom Post Types. Here’s an example:


In this case the post type’s name is wprss_feed so you just need to replace that with your custom post type’s name, and also replace the website domain name of course. That’s all there is to it.

Adding Custom Post Types to Site’s Main Feed

So how can we add all Custom Post Types to our main WordPress RSS feeds?

Insert the following code into your theme’s functions.php file:

function myfeed_request( $qv ) {
    if ( isset( $qv['feed'] ) ) {
        $qv['post_type'] = get_post_types();
    }
    return $qv;
}
add_filter( 'request', 'myfeed_request' );

Adding Specific Custom Post Types to Site’s Main Feed

This code adds custom post type to the main RSS feed for your site, which usually consists only of ‘posts’.

What if you want to choose particular custom post types to add to your site’s feed? That can also be done:

function myfeed_request( $qv ) {
    if ( isset( $qv['feed'] ) && !isset( $qv['post_type'] ) ) {
    $qv['post_type'] = array( 'post', 'project', 'website' );
    }
    return $qv;
}
add_filter( 'request', 'myfeed_request' );

In this code we’ve added an array that modifies the post types that will be shown in the main RSS feed. We are showing the default posts, projects and websites.

Adding Extra Feeds for Custom Post Types

What if you want to add more feeds to your site? Lets say I have a Project custom post type and would like to make available the latest projects my company has launched via  a specific RSS feed that doesn’t include any other content from my site. Here’s how to do it:

This code adds a feed for every post type. If you’d like to only generate an extra feed for a specific custom post type, you’d have to paste this code instead:

add_action( 'wp_head', 'wprss_my_feeds' );

function my_cpt_feeds() {
    $post_types = array('project');
    foreach( $post_types as $post_type ) {
        $feed = get_post_type_archive_feed_link( $post_type );
        if ( $feed === '' || !is_string( $feed ) ) {
            $feed =  get_bloginfo( 'rss2_url' ) . "?post_type=$post_type";
        }
        printf(__('<link rel="%1$s" type="%2$s" href="%3$s" />'),"alternate","application/rss+xml",$feed);
    }
}

Generating Multisite Feeds

If you want to create a global feed from your WordPress Multisite installation, there’s a plugin that does it for you.

Conclusion

In this post, we’ve discussed the topic of generating RSS feeds. If you are also interested in ways that you can import RSS feeds from other websites, our WP RSS Aggregator plugin will probably cover all your needs.

Jean Galea is an investor, entrepreneur, and writer. He is the founder of WP Mayor, and the plugins WP RSS Aggregator and Spotlight. He also runs the Good Life Collective. Connect with him on X or visit jeangalea.com.

If you purchase through a link on our site, we may earn a commission.

3 Responses

  1. The code wasn’t working for me until I created at least one ‘default post type’ post.

  2. This worked great for adding my custom posts to my Mailchimp RSS-to-Email newsletter. Thanks!

Latest Articles from the Blog

Stay updated with WP Mayor's newsletter.

Discover the best and latest tools and services in WordPress every month.