Create Post Only For Your RSS Subscribers in WordPress

Posted in Wordpress

To benefit their regular readers, blog owners some times need to create posts only for those who subscribe to their RSS Feed. The benefit might be some promotional scheme, freebies or discount announcement that you only want to provide to your regular RSS subscribers. Here’s how to do it if you have a WordPress powered blog.

1. First of all, you need to define a category specifically for posts that are to be displayed only in RSS Feed. Let’s say you define a new category ‘RSS Only’ to handle RSS only posts.

After defining the category note down its category ID. To know category ID of the category, open the edit category page for that category and in the address bar of the page, you can find category ID.

2. Now open your theme’s functions.php file in an editor or use the theme editor in WordPress admin. If your theme does not have functions.php file, create one. Add the following code to your functions.php file.

<?php
function excludeCategory($query)
{
	if($query->is_home | $query->is_archive )
	$query->set('cat','-3');
	return $query;
}
add_filter('pre_get_posts', 'excludeCategory'); 
?>

pre_get_posts is a WordPress filter which is run before fetching posts from the database. What we’ve done here is to exclude the category defined above from all pages except RSS Feed. Remember to use the negative sign before category ID to exclude that category.

Now whenever you add a post to this category, it won’t show up on your blog’s homepage, category page, tag page or any other archive page but will be visible in your RSS Feed exclusively for your RSS subscribers.

The post page would however be there as it is for every other post but the post won’t show up anywhere else on your blog.

3. And lastly, if you are using wp_list_categories function to display categories list somewhere in your theme, you need to pass a parameter to wp_list_categories to exclude category meant only for RSS feeds from displaying.

wp_list_categories('exclude=3');

And if wp_list_categories already has parameters then append the exclude parameter using & sign.

wp_list_categories('show_count=1&exclude=3');

Now you can create special posts meant only for your RSS readers. If you know or use some other method of creating RSS only posts, feel free to share them in comments below.

Tags:

Share


Subscribe to Full RSS Feed

RSS FeedIf you found this article useful, then consider subscribing to our RSS Feed or e-mail updates to stay updated with latest Web Design/ Development articles. You can also follow @webdevplus on twitter for latest updates.

9 Responses (Add Your Comment)

  1. Great tutorial. Thanks!

  2. I am no WordPress guru, but it appears as if you are attempting to use the Or comparison operator as(two pipes) || but only have (one pipe) |.

    if($query->is_home | $query->is_archive )

    Maybe should be
    if($query->is_home || $query->is_archive )

    The other option is that one pipe is used as an option selector as in regular expressions, and I am unaware of that option in PHP.

    I am not trying to point out mistakes, just curious what that operator does.

    • @Scott – | operator is a bitwise OR operator that returns result as OR of bits e.g. 1 | 1 will result in 1, 0|1 = 1, 0|0=0, In our case the $query->is_home and $query->is_archive, both return a boolean value either true or false, so we can use bitwise operator as well.

  3. chiti says:

    great, thnx

Comments are closed for this post.