How to Add Custom Post Types to the WP RSS Feed
By default, the main RSS feed of your WordPress site only includes standard posts. WordPress does create extra feeds for custom post types, but what if you wanted those posts in your main feed? In this tutorial, we’ll show you the code needed to include custom post types into the main RSS feed in WordPress.
Adding Custom Post Types to the Main RSS Feed
To add a custom post type to your RSS feed, you need to hook into the request filter, which filters the query variables passed to the default main SQL query for a given page in WordPress. Here’s the code you need to add:
“`php
/**
* Modify the RSS feed post types.
*
* @link https://www.wpexplorer.com/how-to-add-custom-post-types-to-rss-feed/
*/
add_filter( ‘request’, function( $query_vars ) {
if ( isset( $query_vars[‘feed’] ) && ! isset( $query_vars[‘post_type’] ) ) {
$query_vars[‘post_type’] = [ ‘post’, ‘YOUR_CUSTOM_POST_TYPE_NAME’ ];
}
return $query_vars;
} );
“`
Make sure to replace `YOUR_CUSTOM_POST_TYPE_NAME` with the name of your custom post type. This code sets the value of the `post_type` variable and returns an array of all the post types you want to include.
Important RSS Caching Notice: If you add the code snippet to your site and visit your RSS feed, you may not see the custom posts included. WordPress caches your RSS feed to keep it fast. In order to clear the cache, you can create a new post or save any existing post.
Including All Custom Post Types in the Main RSS Feed
If you want to include all custom post types in your RSS feed, you can use the `get_post_types()` function. Here’s an example snippet:
“`php
/**
* Include all custom post types in the main RSS feed.
*
* @link https://www.wpexplorer.com/how-to-add-custom-post-types-to-rss-feed/
*/
add_filter( ‘request’, function( $query_vars ) {
if ( isset( $query_vars[‘feed’] ) && ! isset( $query_vars[‘post_type’] ) ) {
$query_vars[‘post_type’] = get_post_types( [ ‘public’ => true ], ‘names’ );
}
return $query_vars;
} );
“`
Should You Include Custom Posts in the Main RSS Feed?
Whether you should include custom post types in your main RSS feed depends on your site. In most cases, you would keep your custom posts separate from the main feed. Posts such as portfolio, staff, testimonials, and products don’t make sense added to an RSS feed. There are few scenarios where you would need to include custom posts in the main feed.
How to Disable a Custom Post Type RSS Feed?
If you’ve decided to include a custom post type in your main RSS feed, you may want to remove the post type-specific feed. To disable the feed permalink structure for the post type, you can modify the post type arguments. Here’s an example code snippet:
“`php
/**
* Remove RSS feed permalink structure for a custom post type.
*
* @link https://www.wpexplorer.com/how-to-add-custom-post-types-to-rss-feed/
*/
add_filter( ‘register_post_type_args’, function( $args, $post_type ) {
if ( ‘YOUR_CUSTOM_POST_TYPE_NAME’ === $post_type ) {
if ( ! isset( $args[‘rewrite’] ) ) {
$args[‘rewrite’] = [];
}
$args[‘rewrite’][‘feeds’] = false;
}
return $args;
}, 10, 2 );
“`
By disabling the permalink structure for the post type RSS feeds, the URL `your-site.com/post-type/feed/` will return a 404 error. Remember to flush your permalinks by going to Settings > Permalinks and re-saving the settings after adding this code.
To remove the meta links that point to the post type RSS feed, you can use the following code:
“`php
/**
* Remove meta links to a custom post type RSS feed.
*
* @link https://www.wpexplorer.com/how-to-add-custom-post-types-to-rss-feed/
*/
add_action( ‘wp’, function() {
if ( is_post_type_archive( ‘YOUR_CUSTOM_POST_TYPE_NAME’ ) ) {
remove_action(‘wp_head’, ‘feed_links_extra’, 3 );
}
} );
“`
This code will remove the custom post type RSS feed from the archive. Alternatively, you can set the `has_archive` argument when registering your post type to `false` to remove the feed.
In conclusion, adding custom post types to the main RSS feed in WordPress is a straightforward process. However, it’s important to consider whether including custom posts in the main feed is necessary for your site. If you do decide to include them, make sure to disable the specific post type RSS feed and remove the meta links for a better SEO experience.