Index: src/wp-includes/taxonomy.php
===================================================================
--- src/wp-includes/taxonomy.php	(revision 38770)
+++ src/wp-includes/taxonomy.php	(working copy)
@@ -1901,7 +1901,7 @@
 	 */
 	return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
 }
-
+
 /**
  * Add a new term to the database.
  *
@@ -2882,6 +2882,34 @@
 	return true;
 }

+/**
+ * Retrieves the term count for a specific object type.
+ *
+ * @param $term_id
+ * @param $taxonomy
+ * @param $object_type
+ *
+ * @return boolean|int|WP_Error
+ */
+function wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type ) {
+	if ( ! taxonomy_exists( $taxonomy ) ) {
+		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
+	}
+
+	if ( ! is_object_in_taxonomy( $object_type, $taxonomy ) ) {
+		return false;
+	}
+
+	$term_count_meta = get_term_meta( $term_id, '_wp_term_counts', true );
+
+	if ( ! $term_count_meta ) {
+		$term = get_term( $term_id, $taxonomy );
+		return (int) $term->count;
+	}
+
+	return (int) $term_count_meta[ $object_type ];
+}
+
 //
 // Cache
 //
@@ -3363,14 +3391,34 @@

 	foreach ( (array) $terms as $term ) {
 		$count = 0;
+		$term_count_meta = get_term_meta( $term, '_wp_term_counts', true );

+		// Remove any object types that no longer have taxonomy support.
+		$term_count_meta = array_intersect( $term_count_meta, $object_types );
+
 		// Attachments can be 'inherit' status, we need to base count off the parent's status if so.
-		if ( $check_attachments )
-			$count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );
+		if ( $check_attachments ) {
+			$attachment_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );

-		if ( $object_types )
-			$count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
+			$count += $attachment_count;
+			$term_count_meta['attachment'] = $attachment_count;
+		}

+		if ( $object_types ) {
+			foreach ( $object_types as $type ) {
+				$current_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = '" . $type . "' AND term_taxonomy_id = %d", $term ) );
+
+				$term_count_meta[ $type ] = $current_count;
+				$count += $current_count;
+			}
+		}
+
+		if ( 1 < count( $term_count_meta ) ) {
+			update_term_meta( $term, '_wp_term_counts', $term_count_meta );
+		} else {
+			delete_term_meta( $term, '_wp_term_counts' );
+		}
+
 		/** This action is documented in wp-includes/taxonomy.php */
 		do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
 		$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
