Create Post Only For Your RSS Subscribers in WordPress

There are times when blog owners/admins want to give a message (like changing feed URL or about a special giveaway only for them) to their blog’s feed subscribers and don’t really want to show it on their blog or to the direct traffic. There’s a simple trick/hack you can use to achieve that! In today’s simple trick you’ll learn to do that.

**WORKS IN WORDPRESS 3.0**

NOTE: I’ll be using the WordPress installation on my localhost for this tutorial.

For this tutorial, I’ll be using some simple steps (in point form) to illustrate the process:

  • Firstly, we need to make a category specially for the posts that will be displayed in the RSS Feed. On your blog’s/website’s dashboard click on the Posts tab and then on the categories link as shown below:


After that fill in the Add a Category form to make a new category:

Now click on the Add New Category button and you’re good to go the next step!

  • Now we need the category ID of the category you just made. For that, on the Categories page, click on the category you made and you’ll reach the Edit Category page and then and in the address bar of the page, you’ll find the category ID:

(As this picture is too big, I suggest you to click on it to see a full-screen preview!)

  • Now open your theme’s function.php file in an editor or use the theme editor in WordPress admin. Add the following code to your functions.php file:
function excludeCategory($query)
{
	if($query->is_home | $query->is_archive )
	$query->set('cat','-ID');
	return $query;
}
add_filter('pre_get_posts', 'excludeCategory');

If your theme does not have a  functions.php file, then create one (You’ll need FTP access to your blog) and add the following (has the <?php & ?> before and after the code, you’ll get an error if you use the code below in your existing functions.php file):

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

?>

Here 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. In code above don’t forget to replace ID with your category ID. Remember to use the negative sign before category ID (only in the code above) otherwise this code won’t work (it’s to exclude that category).

  • Now, if you or your theme is displaying your category list somewhere on your theme (header or sidebar) using the following function:
wp_list_categories();

then don’t forget to edit that and use the following code instead of that:

wp_list_categories('exclude=ID');

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

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

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. Don’t tag this post to any other category except the category for the RSS feeds otherwise it would show up on the category page!

You can leave a response, or trackback from your own site.

Leave a Reply