WordPress remove post from category

This code sample will allow you to hide posts from one or more categories.

Add the following code sample to your functions.php file.

//Hide posts from a category
function exclude_category(){
	$numargs = func_num_args();
	$arg_list = func_get_args();
	for ($i = 0; $i < $numargs; $i++) {
		$queryString .= '-'.get_cat_ID($arg_list[$i]);
		if($i != ($numargs-1)){
			$queryString .= ',';
		}
    }
	query_posts('&cat='.$queryString);
}

You can use the code by calling the function like this.

<?php exclude_category('Food'); ?>

If you would like to remove multiple categories you can do so by calling the function like this.

<?php exclude_category('Food','Computers','Pictures'); ?>

I am using this technique to hide my code samples from being displayed on my main page. I felt that my site would be better organized if the code samples were in another area of my website instead of on the main page with the rest of the tutorials. All of my code samples are in a special category called “Code”. I placed the exclude_category function before WordPress’s loop call on my index.php file to hide them.