October 13th, 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

  • http://anekdamian.com Anekdamian

    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?

  • http://digitalrenewal.com Trisha

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

  • http://andrewjtalcott.com andrewjtalcott

    Very helpful, thank you.

  • Kiran

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

  • http://twitter.com/PriyankaNM Priyanka Naskar Mjdr

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