WordPress offers several different feeds for your reader, but not always is their desired format available.
In some cases the reader has the desire to receive just specific news of a website and not all of them. WordPress is flexible enough to meet these requirements. How you can implement this via function in a theme is very easy. I will show you some code snippets and explain it to you.
Template tag query_posts
is very powerful and can help you in this matter. There is no change in your core files necessary.
For a better understanding of the concept I will give you some basic information to feeds in WordPress.
WordPress offers four feed formats RDF, RSS 0.92, RSS 2.0 and ATOM 1.0 (since WP V2.*). As a standard, they create feeds in posts, comments and all content in archive – like categories or tags. Some examples for understanding.
- normal feed (Permalinks inaktive) for RDF:
http://example.de/?feed=rdf
- normal feed (Permalinks inaktive) for RSS:
http://example.de/?feed=rss
- normal feed (Permalinks inaktive) for RSS2:
http://example.de/?feed=rss2
- normal feed (Permalinks inaktive) for Atom:
http://example.de/?feed=atom
- normal feed (Permalinks aktive) for the tag example in RSS 2 format:
http://example.de/?feed=rss2&?tag=example
- normal feed (Permalinks aktive) for RDF:
http://example.de/feed/rdf
- normal feed (Permalinks aktive) for RSS:
http://example.de/feed/rss
- normal feed (Permalinks aktive) for RSS2:
http://example.de/feed
- normal feed (Permalinks aktive) for Atom:
http://example.de/feed/atom
- normal feed (Permalinks aktive) for the tag example in RSS 2 format:
http://example.de/tag/example/feed/
Now you like to create a feed based on categories and also create a feed, excluding categories. All examples are based on the RSS2 format, but it’s easy to use any other format for these example.
- Feed of the category with ID 23:
http://example.de/feed?cat=23
- Feed of the category with ID 12, 13, 23:
http://example.de/feed?cat=12&cat=13&cat=33
- Exclude category with ID 23 in this feed:
http://example.de/feed?cat=-23
- Exclude categories with ID 12, 13, 23 in this feed:
http://example.de/feed?cat=-12&cat=-13&cat=-33
Especially the second example shows how you can mix the different categories easily. Very useful if you have a website with quite a lot topics to offer. So you can create feeds for different reader groups.
This form of creating specific feeds is pretty easy but we don’t have a function yet which we could use. But the themes in WordPress allow some kind of extension to use a function for creating specific feeds. You have to put the function in functions.php
of your theme. Here an example:
function fb_cat_exclude($query) {
if ($query->is_feed) {
$query->set('cat','-12,-13,-33');
}
return $query;
}
add_filter('pre_get_posts','fb_cat_exclude');
The above example creates a function which gets feeds from all areas except categories with ID 12, 13 and 33. With the help of filter hooks it gets delivered to the query. And with the help of conditional tag is_feed()
the categories will be only exclude in a feed.
This gives you a pretty powerful tool, the reader gets only the news you like to give him. For all other content he has to visit your website. It is pretty flexible and you can control the content in your feeds. You are able to meet special requirements if you need to.
Comments
5 responses to “Customize Your WordPress-Feeds”
Thank you for the tips on WordPress feeds.
Our pleasure Jen!
Hi, this looks quite interesting. When I get my feeds working, I’ll probably use that.
Would you know how to get my feeds working in wordpress? It keeps saying that the URL doesn’t exist and I don’t know how to create it.
I think, you have an error in your install, you have not content in your feed. Maybe; copy all files and folder of WP new,not overwrite, new copy.
When I place the fb_cat_exclude function in functions.php if excludes the categories for RSS2, RDF and ATOM but not RSS. Any ideas?
Thanks
Jason
function fb_cat_exclude($query) {
if ($query->is_feed) {
$query->set(‘cat’,’-345,-355,-356′);
}
return $query;
}
add_filter(‘pre_get_posts’,’fb_cat_exclude’);