Add custom post types to wordpress main feed

Background

I wanted my custom post types that I created in my last writeup (custom post types with archive page) to appear in my wordpress main feed. By default the custom post types do not get added to the main feed that wordpress creates. In my last write up I showed how I added the necessary rewrite rules in order to have the structure: www.domain.com/custom-p-type/feed. This will create a feed with all of those custom post types. Perfect. But what about my “main” wordpress feed?

What I wanted

What I wanted is for my www.domain.com/feed to include ALL of my sites content. Be it posts, or custom post types that I create. There are a couple of methods I found for doing this. I finally found the solution on the track in this ticket. http://core.trac.wordpress.org/ticket/12943

The solution

Here is what will add your custom post type content to your sites main feed, and leave your regular custom post type feeds specific for their respective content…

function myfeed_request($qv) {
	if (isset($qv['feed']) && !isset($qv['post_type']))
		$qv['post_type'] = get_post_types($args = array(
	  		'public'   => true,
	  		'_builtin' => false
		));
		array_push($qv['post_type'],'post'); 
		return $qv;
}
add_filter('request', 'myfeed_request');

The explanation

Thats it! Done. This code checks to see if the query var ‘feed’ exists, if it does it also checks to see if the ‘post_type’ query var IS NOT set. If the post_type query var is set, this means we are on one of our regular custom post type pages, and we don’t want to modify the way the feeds are genereated, as wordpress handles them out of the box.

So, if we are on a “feed” page, and we don’t have a post_type query var set, we must be on the home page feed (in my case). So we modify the default feed by querying for all custom post types that are not built in, and that are public using the get_post_types() function. We then append the “post” post type to the end with array_push, because I wanted my main feed to include all my custom post type content, as well as my regular post content.

Now I have the structure: mydomain.com/feed/ Which houses all of my sites published content, and each custom post type has its respective feed at mydomain.com/custom-p-type/feed/

Thingabeauty.

21 Responses to “Add custom post types to wordpress main feed”

  1. Jason

    I’m getting this error after pasting it into my functions.php

    “Warning: array_push() [function.array-push]: First argument should be an array” It says the error is on the line number with this statement:

    array_push($qv['post_type'],’post’);

    Any ideas?

    Reply
  2. Ballyhoo Blog

    @Jason,

    I’m not sure why, as I can’t reproduce the error? For some reason it is thinking that $qv['post_type'] is not an array, which is I guess possible if the get_post_types() function call only returns one item.

    So if you only have one custom post type set up, this warning would appear (I guess).

    Changing:

    array_push($qv['post_type'],’post’);

    To:

    $qv['post_type'][] = ‘post';

    Might fix that, but I didn’t test it.

    Or declaring $qv['post_type'] to be an array right before line 3 in that sample code also might fix it… like so…
    $qv['post_type'] = array();

    Doing both is probably the best way.

    function myfeed_request($qv) {
    	if (isset($qv['feed']) && !isset($qv['post_type'])){
    
    		$qv['post_type'] = array();
    		$qv['post_type'] = get_post_types($args = array(
    	  		'public'   => true,
    	  		'_builtin' => false
    		));
    		$qv['post_type'][] = 'post';
    	}
    	return $qv;
    	
    }
    

    Let me know if it works, and I’ll update the post…

    Reply
  3. Jason

    Nice, that cleaned up the error.

    I think I might have an issue with my test install, b/c even error-free, it still doesn’t work for me. Justin Tadlock’s version of this didn’t work for me either. I think it’s b/c I’ve tried so many different custom post type plugins and combinations trying to learn that something got too messed up. One of my earlier test post types doesn’t display anymore! So I bet with a clean install that I haven’t messed up, your new code would work perfectly.

    Thanks for the update!

    Here’s Justin’s that I mentioned BTW: http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page

    Reply
  4. donalyza

    The code you provided is showing all custom post_type in the feed. Would you know the code on how to exclude a particular custom post type?

    Thanks

    Reply
  5. Fifth Element

    Hey thanks for that info, saved a me a lot of head-scratching.

    The filter works fine for me but only in my Atom feed and not in my RSS.

    Any ideas why it’s not showing the custom post types in the RSS feed? I’m using the single post type version of the code.

    Reply
  6. tdriley

    An enclosed mp3 file (ie my podcast) does not appear enclosed in custom posts in the RSS feed, but it does for the normal posts in the feed. Is there any way for this to enclose the MP3 files in custom posts in the feed? With normal posts, I just make a link to it and it appears. My custom post type ‘podep’ does not enclose them when they appear in the feed :(

    Reply
  7. Ballyhoo Blog

    @tdriley,

    I’m not sure. It could be the type of feed (rss,atom,rss2 etc). I’m not sure which types support rich media, but that may the issue.

    Reply
  8. Bassett Providentia

    Hi,

    I have tried to use your code. It does succeed is listing all post types in the feed, but it also lists all posts from all types in the WordPress admin sections for each custom post type, and it prevents the custom post types from being queried by the blog.

    Do you have any ideas?

    Reply
  9. Bassett Providentia

    Please ignore my previous comment. I have just tried it again, using the modified code in on the comment by @Ballyhoo Blog, but with line 9 as: “array_push($qv['post_type'],’post’);”, and it seems to work okay at the moment.

    Well done Ballyhoo Blog!

    Reply
  10. Yotsugi

    I have some problem here, when I publish a new custom post type it doesn’t update the main feed and it update it only after I publish a normal post. I’m using feedburner and must manually “refresh” the feed using this way:

    Login to your FeedBurner account, then click “Edit Feed Details…” at the right top. You don’t have to do anything, just click “Save Feed Details”.

    Voila! Your custom post type feed now appear in the main feed!

    Wonder if this is WordPress bug??

    Reply
  11. Sam

    @tdriley I have exactly the same problem. My mp3 files show if I post them in normal post but if I do it in a custom post type then it does not show. I have searched high and low. Does anyone have an idea how this can be fixed or where we are going wrong with the RSS feeds

    Reply
  12. Harald

    Works great i’d just like to exclude a certain category within the ‘post’ post-type. Any thoughts on how to do that?

    Reply
  13. Greg Turner

    Thanks for the code. It gets me almost to where I am going. How can I have my main RSS feed include ONLY the content for a custom post type?

    Reply
  14. Greg Laws

    HUGE thanks for this! I’ve been banging my head for a solid day trying to figure out a way around this post_type/pagination issue. My sanity has been restored.

    Reply

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>