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 blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

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

More from our blog...

3 Responses

Post a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay updated with WP Mayor's newsletter showcase every week

Stay on top of every new WordPress innovation and latest launches. Receive all our fresh product reviews and expert guides directly in your inbox.

Hosting Survey 2024

Are you happy with your hosting provider or are you over-paying for too little? Have your say below!

"*" indicates required fields

What's the main reason you picked this host?*
How happy are you with your host?*

OPTIONAL: If you'd like to receive the results of this survey, please enter your details below.