I've read this and this but I'm just a basic user.

I have a "technical" blog that more and more people visit.

I'm a freelance, and now, when someone hears about me, he/she wants to search for me, and my blog is #1 (before anything else).

The problem is that it was a personal blog, and now it becomes professionnal. So I would still like to post some articles, but when there are some specific tags (like "games", "family" and so on), I'd like them not to appear on the main page but only under the category filter (http://myblog/?s=game, http://myblog/?s=family would be ok, but not http://myblog/).

How to do this?

3 Answers 3


There is even an example in the WordPress codex here for this: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-3,-8' );
add_action( 'pre_get_posts', 'exclude_category' );

Just modify the category IDs (replace -3,-8 by your category IDs preceded by the minus sign) and put it in your functions.php or your plugin code.

The WordPress codex states the following:

query_posts() is the easiest, but not preferred or most efficient, way to alter the default query. The preferred way is hooking into pre_get_posts and altering the main query.

This way you just alter the main query instead of throwing away the results of the first query and running a second.

  • Ok, but where should I put this code? Mar 21, 2013 at 14:54
  • 1
    functions.php, a plugin, etc
    – Tom J Nowell
    Mar 21, 2013 at 15:12
  • 2
    +1 for using pre_get_posts
    – xsonic
    Mar 22, 2013 at 9:12

This is basic The Loop manipulation. You can edit your Loop to exclude specified categories. You would, for instance, use this modified Loop only in index.php, so on the front page it would exclude those categories, but they'd show on all other template pages.

For more information on using The Loop to exclude categories, check out this WordPress Codex article.


query_posts($query_string . '&cat=-3,-8');

Where -3 means "exclude category with ID 3".

To figure out the ID of a category, navigate to your list of categories in WP-Admin, hover your mouse over a category title and in the URL it leads to is a parameter ID.

  • I would strongly recommend against the user of query_posts
    – Tom J Nowell
    Mar 21, 2013 at 10:38
  • 1
    To clarify, always use WP_Query or the pre_get_posts filter instead, query_posts will wastefully discard the main query, and overwrite it with a second new query
    – Tom J Nowell
    Mar 21, 2013 at 10:39
  • Isn't it difficult to use pre_get_posts and make this work only for the front page? More specifically, I am referring to this. Also, using a new WP_Query will indeed not stomp on the original query, but that is not going to be used anyways (so it's still wasted). Your comment has added value, still. Thanks! Mar 21, 2013 at 10:42
  • 1
    You wouldn't want to use template functions inside pre_get_posts, it's for modifying the query variables, and only modifying the query variables ( which is what you're doing in query_posts, although in a wasteful roundabout way ). pre_get_posts isn't complicated, and the majority of it is copy paste
    – Tom J Nowell
    Mar 21, 2013 at 15:11
  • 2
    I think I'm beginning to understand the differences and why there is so much critique on query_posts(). Specially after reading When should you use WP_Query vs query_posts() vs get_posts()? Mar 21, 2013 at 15:23

if you are using a template you can use the following code

Template Name: Template name
    $recent = new WP_Query("cat=-6");// Excludes the category with id=6  

See wp_query for more info.

  • I've edited your answer, WP_Query can't be used like a function, perhaps you can ellaborate? Also this is essentially a better written version of query_posts, and is still wasting an entire DB query, which is why pre_get_posts is recommended
    – Tom J Nowell
    Mar 21, 2013 at 15:13
  • I have edited the answer...!!!!!
    – MidhuN
    Mar 22, 2013 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.