How to Include Custom Post Types in WordPress RSS Feed

- Advertisement -


How to Include Custom Post Types in WordPress RSS Feed

By default, the main RSS feed of your WordPress site only includes standard posts. This means that any custom post types you have created, such as portfolio items or testimonials, are not included in the main feed. However, if you want to include these custom post types in your RSS feed, there is a way to do it. In this tutorial, we will show you how to add custom post types to the main RSS feed in WordPress.

- Advertisement -

To add a custom post type to your RSS feed, you need to hook into the request filter. This filter is responsible for filtering the query variables passed to the default main SQL query for a given page in WordPress. By modifying the query variables, you can specify which post types should be included in the main feed.

Here is the code you need to add to your WordPress site:

- Advertisement -

“`php
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 in the main feed.

- Advertisement -

However, there is an important notice regarding RSS caching. After adding the code snippet to your site and visiting your RSS feed, you may not see the custom posts included immediately. This is because WordPress caches your RSS feed to keep it fast. In order to clear the cache and see the custom posts in your feed, you can create a new post or save any existing post.

If you want to include all custom post types in your main RSS feed, you can use the ‘get_post_types()’ function. Here is an example snippet:

“`php
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;
} );
“`

This code retrieves all the public post types on your site and includes them in the main feed.

However, it’s important to consider whether including custom post types in the main RSS feed is necessary for your site. In most cases, it is recommended to keep custom posts separate from the main feed. Posts such as portfolio items, staff profiles, testimonials, and products don’t make sense to be included in an RSS feed. There are only a few scenarios where you would need to include custom posts in the main 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 avoid duplication. To disable the feed permalink structure for the post type, you can modify the post type arguments. Here is an example code snippet:

“`php
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. Don’t forget 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
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 -

Stay in Touch

spot_img

Related Articles