WordPress Black Friday & Cyber Monday Deals 2021!

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?

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:

http://www.mysite.com/feed/?post_type=wprss_feed

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
Jean Galea
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.

Consider sharing this post so others can find it:

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on telegram
Share on whatsapp
Share on pocket
Share on email

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week.

Our Sponsors
Solid Affiliate
Contents

2 Responses

Leave a Reply

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

The Beginner’s Handbook
From an introduction on how WordPress works to our recommendations on products and services.
👋 Hey there! We're Gaby and Mark
Every week we share tutorials and genuine reviews of WordPress products and services in our newsletter.
Thousands of people read it!
We’d love for you to join.
We’d love for you to join. Here’s what you’ll be getting:

A single weekly email directly to your inbox.