• I am trying to exclude posts from my index page. I have the below code in my functions.php to accomplish that:

    function my_cat_exclude($query) {
     if ($query->is_home) {
      //$query->set('cat','-1,-3,-4,-5,-6,-7,-8,-9,-10');
      $query->set('cat','11');
     }
    return $query;
    }
    add_filter('pre_get_posts','my_cat_exclude');

    I have 12 categories in my blog. I would like to show posts from only one category (category id 11). I don’t want to show the rest on homepage.

    If I use $query->set('cat','-1,-3,-4,-5,-6,-7,-8,-9,-10'); it doesn’t show posts which shares the same category with category id 11. For example, if a post is assigned to two categories category id 5 and category 11, then, it doesn’t shows that post in my home page. I would like to show all posts belong to category id 11 on my home page.

    If I use $query->set('cat','11'); It shows only category with category id 11. however, it removes the other category posts, custom menus from sidebar widget. Also, removes the menu items from top menu.

    Please help.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Your code looks correct, and if you think about it, if you set a post to two categories, 5 and 11, and you don’t want to show posts from category 5, but you do want to from 11, logically it won’t show up because it’s in 5.

    You’ll need to better how you organize your content, but the code works.

    Thread Starter josep_1982

    (@josep_1982)

    Thanks for your reply. I can understand that. However, if I use $query->set('cat','11'); why all custom menu items are not showing up? Is there anyway to avoid that? any thoughts?

    The code above has nothing to do with custom menu items, it just has to do with the loop and the query.

    You’ll have to post more code if you have something specific to custom menus (better to create a different topic if you’re not talking about the same thing anymore.)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Need help to exclude posts from home page’ is closed to new replies.