Adding Custom Post Types to WP RSS Feed

- Advertisement -

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.

- Advertisement -

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:

- Advertisement -

“`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.

- Advertisement -

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.

- Advertisement -

Related articles

Measuring Influencer Marketing Success & ROI: A Guide

How to Measure Influencer Marketing Success & Return on Investment

What’s the best way to measure your next influencer marketing campaign? Find out with this article.

Fixing Gutenberg Fonts in Classic WordPress Themes

If you are using a Classic WordPress theme it’s possible that the Gutenberg editor looks like sh*t because it’s using default tiny serif fonts that can be difficult to read and write with. This simple guide will show you how you can fix this by customizing the default Gutenberg typography! Method 1: Using Inline CSS […]

The post Fix Gutenberg Fonts in Classic WordPress Themes appeared first on WPExplorer.

Finding Micro Influencers for Your Brand

How to Find Micro Influencers For Your Brand

With a small audience, micro influencers can have a big impact on your marketing strategy. Here‘s how to find them.

WordPress: Remove or Change “Howdy”

WordPress uses the phrase “Howdy!” in various locations of the admin including the toolbar at the top where it reads “Howdy! {User}”. If you find this to be a little unprofessional (like I do) and want to change or remove it, you’ve come to the right place. I will show you how. How to Remove […]

The post How to Remove or Change “Howdy” in WordPress appeared first on WPExplorer.

Disable “Welcome to Block Editor” Popup in WordPress

If every time you open the WordPress Gutenberg Editor you see a “Welcome to the block editor” popup you have probably become annoyed like me. I don’t understand why WordPress displays this notice every time you edit a post in Gutenberg regardless of how many times you close it. There is a track ticket open […]

The post How to Disable the “Welcome to the block editor” Popup in WordPress appeared first on WPExplorer.