Index: wp-admin/includes/class-wp-media-list-table.php
===================================================================
--- wp-admin/includes/class-wp-media-list-table.php	(revision 17614)
+++ wp-admin/includes/class-wp-media-list-table.php	(working copy)
@@ -46,8 +46,34 @@
 		$type_links = array();
 		$_num_posts = (array) wp_count_attachments();
 		$_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
-		if ( !isset( $total_orphans ) )
-				$total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
+		if ( !isset( $total_orphans ) ) {
+			$args = array( 
+				'fields' => 'count',
+				'post_type' => 'attachment', 
+				'post_status' => 'any',  /* If legacy filtering is applied below, pass post_status as 'inherit'. */
+				'post_parent' => 0,
+				'suppress_filters' => false,
+				'query_context' => 'attachment_orphans_count',
+			);
+
+			$total_orphans = intval( get_posts( $args ) );
+		}
+
 		$matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
 		foreach ( $matches as $type => $reals )
 			foreach ( $reals as $real )
Index: wp-admin/includes/media.php
===================================================================
--- wp-admin/includes/media.php	(revision 17614)
+++ wp-admin/includes/media.php	(working copy)
@@ -42,9 +42,31 @@
 
 	$post_id = intval($_REQUEST['post_id']);
 
-	if ( $post_id )
-		$attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
+	if ( $post_id ) {
+		$args = array( 
+			'fields' => 'count',
+			'post_type' => 'attachment', 
+			'post_status' => 'any',  /* If legacy filtering is applied below, pass post_status as 'inherit'. */
+			'post_parent' => $post_id,
+			'suppress_filters' => false,
+			'query_context' => 'gallery_attachment_count',
+		);
+
+		$attachments = intval( get_posts( $args ) );
+	}
+		
 	if ( empty($attachments) ) {
 		unset($tabs['gallery']);
 		return $tabs;
Index: plugin.php
===================================================================
--- plugin.php	(revision 17614)
+++ plugin.php	(working copy)
@@ -72,6 +72,32 @@
 }
 
 /**
+ * Hooks a function or method to a specific filter action, specifying a required context for application
+ *
+ * @package WordPress
+ * @subpackage Plugin
+ * @since 3.2
+ * @global array $wp_filter Stores all of the filters added in the form of
+ *	wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)']']
+ * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, it doesn't need to run through that process.
+ *
+ * @param string $tag The name of the filter to hook the $function_to_add to.
+ * @param string $context Context to require for filter application.
+ * @param callback $function_to_add The name of the function to be called when the filter is applied.
+ * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
+ * @param int $accepted_args optional. The number of arguments the function accept (default 1).
+ * @return boolean true
+ */
+function add_context_filter($tag, $context, $function_to_add, $priority = 10, $accepted_args = 1) {
+	global $wp_filter, $merged_filters;
+
+	$idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
+	$wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args, 'context' => $context);
+	unset( $merged_filters[ $tag ] );
+	return true;
+}
+
+/**
  * Check if any filter has been registered for a hook.
  *
  * @package WordPress
@@ -190,7 +216,7 @@
  * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
  * @return mixed The filtered value after all hooked functions are applied to it.
  */
-function apply_filters_ref_array($tag, $args) {
+function apply_filters_ref_array( $tag, $args, $context = '' ) {
 	global $wp_filter, $merged_filters, $wp_current_filter;
 
 	$wp_current_filter[] = $tag;
@@ -216,7 +242,7 @@
 
 	do {
 		foreach( (array) current($wp_filter[$tag]) as $the_ )
-			if ( !is_null($the_['function']) )
+			if ( !is_null($the_['function']) && ( empty($the_['context']) || ( $context == $the_['context'] ) ) )
 				$args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
 
 	} while ( next($wp_filter[$tag]) !== false );
Index: wp-includes/query.php
===================================================================
--- wp-includes/query.php	(revision 17614)
+++ wp-includes/query.php	(working copy)
@@ -1928,6 +1928,8 @@
 
 		if ( !isset($q['suppress_filters']) )
 			$q['suppress_filters'] = false;
+		
+		$context = isset( $q['query_context'] ) ? $q['query_context'] : '';
 
 		if ( !isset($q['cache_results']) ) {
 			if ( $_wp_using_ext_object_cache )
@@ -2001,6 +2003,9 @@
 			case 'id=>parent':
 				$fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
 				break;
+			case 'count':
+				$fields = "COUNT(*)";
+				break;
 			default:
 				$fields = "$wpdb->posts.*";
 		}
@@ -2167,7 +2172,7 @@
 		}
 
 		// Allow plugins to contextually add/remove/modify the search section of the database query
-		$search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
+		$search = apply_filters_ref_array('posts_search', array( $search, &$this ), $context );
 
 		// Taxonomies
 		if ( !$this->is_singular ) {
@@ -2483,8 +2488,8 @@
 		// Apply filters on where and join prior to paging so that any
 		// manipulations to them are reflected in the paging by day queries.
 		if ( !$q['suppress_filters'] ) {
-			$where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
-			$join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
+			$where = apply_filters_ref_array('posts_where', array( $where, &$this ), $context );
+			$join = apply_filters_ref_array('posts_join', array( $join, &$this ), $context );
 		}
 
 		// Paging
@@ -2549,16 +2554,16 @@
 		// Apply post-paging filters on where and join.  Only plugins that
 		// manipulate paging queries should use these hooks.
 		if ( !$q['suppress_filters'] ) {
-			$where		= apply_filters_ref_array( 'posts_where_paged',	array( $where, &$this ) );
-			$groupby	= apply_filters_ref_array( 'posts_groupby',		array( $groupby, &$this ) );
-			$join		= apply_filters_ref_array( 'posts_join_paged',	array( $join, &$this ) );
-			$orderby	= apply_filters_ref_array( 'posts_orderby',		array( $orderby, &$this ) );
-			$distinct	= apply_filters_ref_array( 'posts_distinct',	array( $distinct, &$this ) );
-			$limits		= apply_filters_ref_array( 'post_limits',		array( $limits, &$this ) );
-			$fields		= apply_filters_ref_array( 'posts_fields',		array( $fields, &$this ) );
+			$where		= apply_filters_ref_array( 'posts_where_paged',	array( $where, &$this ), $context );
+			$groupby	= apply_filters_ref_array( 'posts_groupby',		array( $groupby, &$this ), $context );
+			$join		= apply_filters_ref_array( 'posts_join_paged',	array( $join, &$this ), $context );
+			$orderby	= apply_filters_ref_array( 'posts_orderby',		array( $orderby, &$this ), $context );
+			$distinct	= apply_filters_ref_array( 'posts_distinct',	array( $distinct, &$this ), $context );
+			$limits		= apply_filters_ref_array( 'post_limits',		array( $limits, &$this ), $context );
+			$fields		= apply_filters_ref_array( 'posts_fields',		array( $fields, &$this ), $context );
 
 			// Filter all clauses at once, for convenience
-			$clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
+			$clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ), $context );
 			foreach ( $pieces as $piece )
 				$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
 		}
@@ -2568,16 +2573,16 @@
 
 		// Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
 		if ( !$q['suppress_filters'] ) {
-			$where		= apply_filters_ref_array( 'posts_where_request',		array( $where, &$this ) );
-			$groupby	= apply_filters_ref_array( 'posts_groupby_request',		array( $groupby, &$this ) );
-			$join		= apply_filters_ref_array( 'posts_join_request',		array( $join, &$this ) );
-			$orderby	= apply_filters_ref_array( 'posts_orderby_request',		array( $orderby, &$this ) );
-			$distinct	= apply_filters_ref_array( 'posts_distinct_request',	array( $distinct, &$this ) );
-			$fields		= apply_filters_ref_array( 'posts_fields_request',		array( $fields, &$this ) );
-			$limits		= apply_filters_ref_array( 'post_limits_request',		array( $limits, &$this ) );
+			$where		= apply_filters_ref_array( 'posts_where_request',		array( $where, &$this ), $context );
+			$groupby	= apply_filters_ref_array( 'posts_groupby_request',		array( $groupby, &$this ), $context );
+			$join		= apply_filters_ref_array( 'posts_join_request',		array( $join, &$this ), $context );
+			$orderby	= apply_filters_ref_array( 'posts_orderby_request',		array( $orderby, &$this ), $context );
+			$distinct	= apply_filters_ref_array( 'posts_distinct_request',	array( $distinct, &$this ), $context );
+			$fields		= apply_filters_ref_array( 'posts_fields_request',		array( $fields, &$this ), $context );
+			$limits		= apply_filters_ref_array( 'post_limits_request',		array( $limits, &$this ), $context );
 
 			// Filter all clauses at once, for convenience
-			$clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
+			$clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ), $context );
 			foreach ( $pieces as $piece )
 				$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
 		}
@@ -2593,7 +2598,7 @@
 
 		$this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
 		if ( !$q['suppress_filters'] )
-			$this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
+			$this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ), $context );
 
 		if ( 'ids' == $q['fields'] ) {
 			$this->posts = $wpdb->get_col($this->request);
@@ -2610,12 +2615,18 @@
 
 			return $r;
 		}
+		
+		if ( 'count' == $q['fields'] ) {
+			$this->posts = $wpdb->get_var($this->request);
 
+			return $this->posts;
+		}
+
 		$this->posts = $wpdb->get_results($this->request);
 
 		// Raw results filter.  Prior to status checks.
 		if ( !$q['suppress_filters'] )
-			$this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
+			$this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ), $context );
 
 		if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
 			$cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
@@ -2631,9 +2642,9 @@
 		}
 
 		if ( !$q['no_found_rows'] && !empty($limits) ) {
-			$found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
+			$found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ), $context );
 			$this->found_posts = $wpdb->get_var( $found_posts_query );
-			$this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
+			$this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ), $context );
 			$this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
 		}
 
@@ -2720,7 +2731,7 @@
 		}
 
 		if ( !$q['suppress_filters'] )
-			$this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
+			$this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ), $context );
 
 		$this->post_count = count($this->posts);
 
