How to exclude a category from your main wordpress index and RSS feed

Few days ago I was experimenting with Twitter tools, so I found that the Tweet Digest posts created by Twitter tools are being listed with my other posts in the main index of my blog. Twitter digests posts were not looking good to me as the Twitter digests posts from Twitter Tools were long and they were making my other posts unfindable. So I excluded that Category in the main index listing of my blog. But just yesterday I added my blog to FriendFeed and this is when I figured out that I should also be disabling the ‘Twitter Archive‘ category in the RSS feed of my blog. So I did some research and found very helpful trick from wpbeginner. Below I will show you how I used the code from wpbegginer not by just copy-pasting it but by tweaking to fit my need. Before you read further let me just remind you what I am trying to do here. I am trying to disable ‘Twitter Archive’ category (with category id 27) in the main blog listing as well as in my default RSS feed. So to simply exclude the Category from RSS feed as mentioned on wpbeginner I had to include the following code in my theme’s functions.php.

function catExclude($query) {
    if ($query->is_feed) {
         $query->set('cat','-27'); //Don't forget to change the category ID
    }
    return $query;
}
add_filter('pre_get_posts','catExclude');

But the above code proved to be not working for my case, as I wanted to exclude the category without breaking customized feed or category based feeds of my blog. As the above code is explicitly setting category with category ID ’27’ to be excluded, it doesn’t matter if you use the customize feed URL to exclude other categories. In simple words the above filter does not preserve the values of cat in the query string, it just sets the new value regardless what was provided in the url to get the customized feed. So I just can’t use this function, being a perfectionist I don’t like breaking things to build or do something new. So I did some more research on the above code and found that I can do play with $query object that is being passed to catExclude function above.

After reading about the $query object of class WP_Query I found that there is a function which allows to get the value of current variable in query string. So I just added an if condition to the above code to check if there is already a value set for ‘cat’ in query string, if there is already a value set then I don’t modify the value, else I just exclude my ‘Twitter Archive’ category. By using this check of an existing cat value I successfully excluded ‘Twitter Archive’ category form default RSS feed with out breaking the functionality and I can still have separate feeds for all my categories. Not only that but my ‘Twitter Archive’ category can also have its own dedicated feed without showing up in main default RSS feed. Below is the final code that I used to exclude the ‘Twitter Archive’ category from my RSS feed.

function catExclude($query) {
    if ($query->is_feed) {
       if (!$query->get('cat'))  {
            $query->set('cat','-27'); //Don't forget to change the category ID
       }
    }
    return $query;
}
add_filter('pre_get_posts','catExclude');

Now there is one last thing I modified in the above code to use it to exclude category in the main index of my wordpress blog. As I mentioned earlier about my previous post on the topic, that describes another way of achieving the same thing. But in case you don’t want to use that method and want to keep your edits to the single file and exclude your selected categories from both the main index and RSS feed then you use the below code.

function catExclude($query) {
    if ($query->is_feed || $query->is_home) {
       if (!$query->get('cat'))  {
            $query->set('cat','-27'); //Don't forget to change the category ID
       }
    }
    return $query;
}
add_filter('pre_get_posts','catExclude');

I hope you already have noticed the use of is_home in if condition. If you will use the code above with is_home condition then you don’t need to use the method that I mentioned in my previous post about excluding category in main index of wordpress blog. Now the last thing you might want to do with the above code that is to exclude multiple categories. Specify the multiple categories as below in your $query->set call.

$query->set('cat','-27,-30,-32');

I know the above code samples might not make much sense to non PHP programmers, so if you need to edit your wordpress blog and does not know how, you can hire me. You can also find my contact information on the contact page.

If you know a better way of doing the same thing, kindly share with us in the comments. Even if you don’t know a better way, let me know in comments what do you think about this trick and if you like it then don’t forget to share it with your friends.

How to use oauth-python-twitter

Using OAuth for authentication is quite different then using Basic Auth in many ways, first major difference is that one don’t know the username until it is requested from twitter after completing the authentication process.

Details

You will need following information to start.

  • Consumer Key
  • Consumer Secret

If you do not have above, you can request them from here: http://twitter.com/oauth_clients

The following are the steps that should be performed to authenticate user and get user information from twitter, the rest is same as using python-twitter.

  • Get the Request Token from twitter
  • Get Authorization URL
  • Get the Access Token from twitter
  • Get user information

Get the Request Token from twitter

twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET)
 
request_token = twitter.getRequestToken()

Get Authorization URL

using same request_token from previous step

authorization_url = twitter.getAuthorizationURL(request_token)

Now send the user to authorization URL, for allowing access to the application.

Get the Access Token from twitter

Once the user return from twitter, we need to request the access_token from twitter for futher aunthenticated api calls on behalf of user.

NOTE: Need to create the new instance of OAuthApi using request_token from first step.

twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, request_token)
 
access_token = twitter.getAccessToken()

Get user information

This api call was not present in python-twitter and the user info call expected that there we know the username before making call, but this is not the case with OAuth, so we have to request User Information so we can use it in our application.

NOTE: Need to create the new instance of OAuthApi using access_token from last step.

twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
 
user = twitter.GetUserInfo()

Now use the rest of python-twitter api calls as you used to use them with basic authentication, for more information see the wiki at:http://code.google.com/p/python-twitter