Add Content Exclusively to a Custom Post Type Feed

Introduction

If I wanted to add some content at the end of an RSS feed entry, I could write something like this:

// quick PHP 5.3+ example
add_filter('the_content', function($content) {
    // If this is not a feed, leave now.
    if ( !is_feed() ) return $content;

    // Add content to the feed entry.
    $content .= 'Thank you for subscribing to the feed.';
});

What I Want to Do

The above works fine; however, that will add “Thank you for subscribing to the feed.” to every feed entry. If I wanted to add that line only if it’s a feed for a certain custom post type, how would I do that?

For example, if I had a custom post type called 'books', I could access the feed link for that by going to example.com/feed/?post_type=books. So instead of saying, 'Thank you for subscribing to the feed.' I would like to say, 'Thank you for subscribing to the books feed.' Likewise with feeds of other post types.


I figured that passing an argument into is_feed( $feeds ) could be the answer, but the codex documentation doesn’t seem to be very clear to me on what to pass exactly. It just says, “feed types”?

$feeds
(array/string) (optional) Feed type(s) to check.
Default: None

Is that a URL to a feed? A slug of something perhaps? “RSS 2.0” etc.? (Or maybe that has nothing to do with my problem.)

The only information I can seem to find is how to access the custom post type’s feed in the first place (e.g. example.com/feed/?post_type=books)—not how to add content exclusively to that feed.

Thanks for any help.

Solutions Collecting From Web of "Add Content Exclusively to a Custom Post Type Feed"

First you can use more specific the_content_feed hook, so your code doesn’t have to run in unrelated contexts.

The “type” refers to feeds (not posts), so it would be something like rss2 and it will be same across post types.

It is a little unobvious, but feeds are also kind of a Loop, same as on front end. So same APIs typically work just fine.

Your check can be something like if ( 'books' === get_post_type() ).