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…

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.

Category: General

Tags: , , , ,

- June 21, 2010

Comments

  1. 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?

  2. Ballyhoo Blog
    July 4, 2010 - 8:44 pm

    @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.

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

  3. 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

  4. 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

  5. @donalyza

    Yeah, that’s pretty easy. You can call more custom arguments on the get_post_types function, and query for specific post types, excluding others.

    http://codex.wordpress.org/Function_Reference/get_post_types

    That link has a list of available arguments that you could filter by: Such as query_var => ‘something’

  6. 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.

  7. 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 :(

  8. @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.

  9. Bassett Providentia
    January 12, 2011 - 5:16 am

    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?

  10. Bassett Providentia
    January 12, 2011 - 5:25 am

    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!

  11. 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??

  12. @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

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

  14. 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?

  15. 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.

  16. Can you not publish this code as a wp plugin? It will make things simpler for a lot of people!

  17. It works exactly as described !
    Thanks for the hack !

  18. This code is working. How do we include image in the feed?

Leave a Reply

Your email address will not be published / Required fields are marked *