The Zeek Blog

We Don’t Write Often, But When We Do... We Do It Here.

add_feed();

23 March

I discussed this function during the lightning round at WordCamp San Diego 2013.

add_feed() adds a custom feed to your WordPress site. I discovered this function recently when I needed to create a feed that uses a custom query. Specifically, a client needed a feed that pulled together content in a single category from multiple custom post types. The default WordPress category feeds will not pull content from multiple custom post types.

The code below can be dropped into functions.php in your theme folder or it could be rolled into a plugin.

[php]
add_feed( ‘games’ , ‘make_games_feed’ );

function make_games_feed(){
$args = array(
‘post_type’ => array(‘post’,'project’),
‘cat’ => get_cat_id(‘games’)
);

query_posts($args);
include_once(ABSPATH . ‘wp-includes/feed-rss2.php’);
wp_reset_query();
}
[/php]

After this code is added to your site, you must flush the rewrite rules to get it to take effect. A quick way to do this is to go to SETTINGS -> PERMALINKS and click SAVE CHANGES. You don’t have to actually make a change.