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.
I tried ! sign also, but no luck.

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

$id = get_cat_id('Idhar-Udhar'); 
$q = "cat=-" . $id;
  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

  • 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?

  • hurray, it works and it was easy! thanks for saving me from lots of stress :D :D

  • Very helpful, thank you.

  • Kiran

    Thanks alot its work for me……..very nice

  • Thanks. Used a slight variation of this for querying posts outside the loop, and it worked!