Oct 13, 2010

Exclude Posts by Category Name from Home page in WordPress

I have created a new category ‘Idhar-Udhar’, But I don’t want to show posts related to this category on home page. I saw “Function Reference/query posts”. there is parameter category_name which displays posts from only one category by name. There is no parameter to exclude posts by category_name. I tried -(minus sign) in following way, but not worked.
query_posts(‘category_name=-Idhar-Udhar’);
I tried ! sign also, but no luck.
query_posts(‘category_name!=Idhar-Udhar’);


Finally, I have done this using category ID. See following:

$id = get_cat_id('Idhar-Udhar'); 
$q = "cat=-" . $id;
query_posts($q); 
  if (have_posts()) : while (have_posts()) : the_post();

First get ID using get_cat_id function then using cat parameter with -(minus sign) and category ID to exclude.

Hope, It helps

5 comments

  1. Great! I've been looking for this for a while noe. Thank you very much for posting.
    But what if I want to limit the number of posts to be displayed in the “query_posts” line? how do i do it?

Leave a Reply

Your email address will not be published. Required fields are marked *