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.
3 Responses
The code wasnβt working for me until I created at least one βdefault post typeβ post.
This worked great for adding my custom posts to my Mailchimp RSS-to-Email newsletter. Thanks!
Nice article information in generate feed thanks lot