Often your WordPress website will have custom post types, in addition to your normal blog posts, for content such as books, staff, real estate listings, events, portfolio. Sometimes you don’t want this content to go out in your regular blog RSS feed which is the default behavior. But often, the custom post types are valuable content that you would like to share with readers who subscribe to your blog RSS feed or to automatically send out to your email list using the RSS feed in your newsletter service. This WordPress tutorial shows how you can add custom post type content to your WordPress RSS feed.
It’s best to add custom post types individually, because there may be some custom post types generated by some plugins that you would not want to go out in your feed.
First, look at your current feed to see what it’s showing. You want to add /feed to the url for your site in your browser. So http://yoursite.com/feed. If you look at your feed url in Firefox, it will show a list of your latest blog titles with a snippet using the Live Bookmarks feature. If you use, Chrome, you will see a text file with code. You can scan this code for <title> tags, to see your blog post titles.
To add your custom post type to this feed, open your child theme functions.php file in a text editor. Add this code section at the bottom.
<?php
// Note: Add only code below to your functions.php
// Add custom post types - cpt1 and cpt2 to main RSS feed.
function mycustomfeed_cpt_feed( $query ) {
if ( $query->is_feed() )
$query->set( 'post_type', array( 'post', 'cpt1', 'cpt2' ) );
return $query;
}
add_filter( 'pre_get_posts', 'mycustomfeed_cpt_feed' );
You want to substitute the name of your custom post type for cpt1 and cpt2. You can add as many custom post types as you like.
Then check http://yoursite.com/feed to see if the added post types are in your feed.
Suppose you have an events custom post type. You would see a list of the events on a page with url http://yoursite.com/events. If you also have a custom post type ‘books’, you would see a list of the books at url http://yoursite.com/books.
If you wanted every new event and every new book to go out in your RSS, then the code that you would place in your functions.php would look like this:
<?php
// Note: Add only code below to your functions.php
// Add custom post types - events and books to main RSS feed.
function mycustomfeed_cpt_feed( $query ) {
if ( $query->is_feed() )
$query->set( 'post_type', array( 'post', 'events', 'books' ) );
return $query;
}
add_filter( 'pre_get_posts', 'mycustomfeed_cpt_feed' );
If you only wanted the events to go in your feed, then you would only list – 'post', 'events'
– in the array list.
If you are using a plugin to create your custom post type, like Sugar Event Calendar, for example, you will need to find the custom post type slug – sc_event
. If it’s not listed on the WordPress plugin download page or the Readme file for the plugin, you may need to ask in the support forum.
What custom post types will you add to your WordPress RSS feed?
Leave a Reply