Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Add Post Types To RSS Feed

DZone's Guide to

Add Post Types To RSS Feed

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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;
}

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}