Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

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.

share|improve this answer
    
Ok, but where should I put this code? –  Olivier Pons Mar 21 '13 at 14:54
1  
functions.php, a plugin, etc –  Tom J Nowell Mar 21 '13 at 15:12
2  
+1 for using pre_get_posts –  xsonic Mar 22 '13 at 9:12
add comment

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.

Basically

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.

share|improve this answer
    
I would strongly recommend against the user of query_posts –  Tom J Nowell Mar 21 '13 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 '13 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! –  Marc Dingena Mar 21 '13 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 '13 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()? –  Marc Dingena Mar 21 '13 at 15:23
show 1 more comment

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

<?php
/*
Template Name: Template name
  */
?>
<?php 
    get_header();
    $recent = new WP_Query("cat=-6");// Excludes the category with id=6  
    while($recent->have_posts()):$recent->the_post();
    the_title() 
    the_content();  
    endwhile;
    get_footer();
?>

See wp_query for more info.

share|improve this answer
    
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 '13 at 15:13
    
I have edited the answer...!!!!! –  MidhuN Mar 22 '13 at 5:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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