HTML5 Zone is brought to you in partnership with:

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5. Paul is a DZone MVB and is not an employee of DZone and has posted 134 posts at DZone. You can read more from them at their website. View Full User Profile

Add Post Types To RSS Feed

05.07.2014
| 5821 views |
  • submit to reddit

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;
}
Published at DZone with permission of Paul Underwood, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)