Paulund
2014-05-07 #wordpress

Add Custom Post Types To RSS Feed

By default WordPress comes with an in-built RSS feed allowing your visitors to subscribe to your RSS feeds to be alerted to your new articles. This will normally consist of just the normal blog posts, but as WordPress is a CMS you could have other content types that would suit being in your RSS feed. WordPress allows you to create different post types, out of the box it comes with two posts and pages. But with a bit of code you can create a brand new post type for anything you want. The below code will look into getting other post types into your website RSS feed. This first code block will allow you to change the RSS feed to include all posts types in your RSS. First we check to see if we are on the RSS feed page, if so then change the post type variable and replace it with get_post_types(), which returns an array of all post types. Next time you navigate to the RSS feed you will see other posts types added to the feed.


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

But if you don't want to display all post types but only have certain post types then you need to change the code and pass in an array of post types which you want to display in the RSS feed. When a post type is created it needs to register with an ID, you will use this in the array to build up the RSS feed.

add_filter('request', 'pu_specific_post_type_custom_feed');
function pu_specific_post_type_custom_feed( $vars ) 
{
    $post_type_list = array( 'post', 'downloads' );
    if ( isset( $vars['feed'] ) AND !isset( $vars['post_type'] ) ) {
        $vars['post_type'] = $post_type_list;
    }
    return $vars;
}