How to add your custom post type to RSS feeds

Out of the box, WordPress does not include custom post types in RSS feeds. Because of this, you need to add the post types to the query yourself.

You will add the following snippets to your theme’s functions.php file or if you want, to a custom plugin build just for your own website.

Don’t want to worry about coding this yourself?

This is available via user interface with our premium CPTUI-Extended product.

Adding all CPTUI post types to the RSS feeds.

This example requires Custom Post Type UI 1.3.0+ due to cptui_get_post_type_slugs() function.

function my_cptui_add_post_types_to_rss( $query ) {
	// We do not want unintended consequences.
	if ( ! $query->is_feed() ) {
		return;    
	}

	$cptui_post_types = cptui_get_post_type_slugs();

	$query->set(
		'post_type',
		array_merge(
			array( 'post' ),
			$cptui_post_types
		)
	);
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_rss' );

In this code, we check for if we’re querying for a feed. If so, then we fetch an array of all CPTUI based post type slugs, and merge it into an array with the ‘post’ post type. With that array constructed, we pass it into the query parameters so that WordPress knows to query for all these post types.

Adding only some CPTUI post types to the RSS feeds.

function my_cptui_add_post_types_to_rss( $query ) {
	// We do not want unintended consequences.
	if ( ! $query->is_feed() ) {
		return;    
	}

	// Replace these slugs with the post types you want to include.
	$cptui_post_types = array( 'my_post_type', 'my_other_post_type' );

	$query->set(
  		'post_type',
		array_merge(
			array( 'post' ),
			$cptui_post_types
		)
	);
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_rss' );

This snippet will be very much like the first one, except we’re only including specific post types from CPTUI.

Updated Jan 17, 2023 5:53 PM