Exclude posts from a WordPress Loop

Sometime you want to exclude posts from the loop.

In this example, I exclude posts that only in sub-categories or posts with a custom field value.

<?php 
	// get a list of categories
	$args = array(
		'type'				=> 'post',
		'hide_empty'		=> 1,
		'hierarchical'		=> 1,
		'taxonomy'			=> 'category',
		'pad_counts'		=> false 
	); 
	$subcategories = get_categories( $args );
	
	// separate a list of parent and sub categories
	$parentcategories_array = array();
	$subcategories_array = array();
	foreach ( $subcategories as $subcategory ) {
		// check if parent category (or any other condition here)
		if ($subcategory->parent == 0)
		{
			$parentcategories_array[] = $subcategory->term_id;
		}
		else
		{
			$subcategories_array[] = $subcategory->term_id;
		}
	}
	$subcategories_list = implode(",",$subcategories_array);	// make this a comma delimited string
	
	// get excluded posts
	// in this case, all posts in a sub category
	$args = array(
		'post_type'        => 'post',
		'post_status'      => 'publish'
		);
	$posts_array = get_posts( $args );
	$excludeposts_array = array();
	foreach ( $posts_array as $post_eval ) {
		// check that it is not also in a parent category and custom field not set to true
		$custom_exclude_from_loop = get_post_meta( $post->ID, 'your-custom-field', true ); // get from custom field types plugin
		if ( !in_category( $parentcategories_array, $post_eval ) || $custom_exclude_from_loop) {
			// They have long trunks...
			$excludeposts_array[] = $post_eval->ID;
		} 	
	}
	
	// get pagination information
	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	
	// create new loop with this query
	if ( is_home() ) {
		// This is the  home page
		$args=array(
		  'paged' => $paged,
		  'post__not_in' => $excludeposts_array,
		  'post_type' => 'post',
		  'post_status' => 'publish',
		);
		// run query
		query_posts($args);
	} else if (is_category()) {
		// This is a category page
		$cat_id = get_query_var('cat');	// current category
		$args=array(
		  'cat' => $cat_id,
		  'post__not_in' => $excludeposts_array,
		  'post_type' => 'post',
		  'post_status' => 'publish',
		);
		// run query
		query_posts($args);
	}
?>

<?php if ( have_posts() ) : ?>

etc....