Index: src/wp-includes/taxonomy.php
===================================================================
--- src/wp-includes/taxonomy.php	(revision 39545)
+++ src/wp-includes/taxonomy.php	(working copy)
@@ -2897,6 +2897,55 @@
 	return true;
 }
 
+/**
+ * Retrieves the term count for a specific object type.
+ *
+ * @param int    $term_id Term ID.
+ * @param string $taxonomy Taxonomy name.
+ * @param string $object_type Object type.
+ *
+ * @return WP_Error|bool|int WP_Error if invalid taxonomy is passed.
+ *                           False if object is not in taxonomy.
+ *                           Object term count otherwise.
+ */
+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 = get_term( $term_id, $taxonomy );
+
+	if ( 0 === $term->count ) {
+		return 0;
+	}
+
+	$taxonomy_object = get_taxonomy( $taxonomy );
+
+	if ( 1 < count( $taxonomy_object->object_type ) ) {
+		if ( $term_object_count = get_term_meta( $term_id, '_wp_object_count_' . $object_type, true ) ) {
+			return (int) $term_object_count;
+		} else {
+			// When a count for an object type does not exist, if any other meta caches exist we can assume 0.
+			foreach ( $taxonomy_object->object_type as $type ) {
+				if ( $test_meta = get_term_meta( $term_id, '_wp_object_count_' . $type, true ) ) {
+					return 0;
+				}
+			}
+
+			// No other meta caches existed. Count and try again.
+			if ( wp_update_term_count_now( array( $term_id ), $taxonomy ) ) {
+				return wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type );
+			}
+		}
+	} else {
+		return $term->count;
+	}
+}
+
 //
 // Cache
 //
@@ -3372,8 +3421,9 @@
 
 	$object_types = (array) $taxonomy->object_type;
 
-	foreach ( $object_types as &$object_type )
+	foreach ( $object_types as &$object_type ) {
 		list( $object_type ) = explode( ':', $object_type );
+	}
 
 	$object_types = array_unique( $object_types );
 
@@ -3382,19 +3432,50 @@
 		$check_attachments = true;
 	}
 
-	if ( $object_types )
+	if ( $object_types ) {
 		$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
+	}
 
 	foreach ( (array) $terms as $term ) {
 		$count = 0;
 
+		$term_count_meta = array();
+
+		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 = %s AND term_taxonomy_id = %d", $type, $term ) );
+
+				$count += $current_count;
+				$term_count_meta[ $type ] = $current_count;
+			}
+		}
+
 		// 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;
 
+			// Re-add attachment so the meta gets saved below.
+			$object_types[] = 'attachment';
+		}
+
+		// Save individual counts for each object type in term meta.
+		if ( 1 < count( $term_count_meta ) ) {
+			foreach ( $object_types as $type ) {
+				if ( empty( $term_count_meta[ $type ] ) ) {
+					delete_term_meta( $term, '_wp_object_count_' . $type );
+				} else {
+					update_term_meta( $term, '_wp_object_count_' . $type, (int) $term_count_meta[ $type ] );
+				}
+			}
+		} else {
+			foreach ( $object_types as $type ) {
+				delete_term_meta( $term, '_wp_object_count_' . $type );
+			}
+		}
+
 		/** 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 ) );
Index: tests/phpunit/tests/term/getTerms.php
===================================================================
--- tests/phpunit/tests/term/getTerms.php	(revision 39545)
+++ tests/phpunit/tests/term/getTerms.php	(working copy)
@@ -228,9 +228,9 @@
 		), $terms_id_slug );
 	}
 
- 	/**
+	/**
 	 * @ticket 11823
- 	 */
+	 */
 	function test_get_terms_include_exclude() {
 		global $wpdb;
 
@@ -2219,6 +2219,100 @@
 		$this->assertNotEquals( 'foo', $found );
 	}
 
+	/**
+	 * @ticket 38280
+	 */
+	public function test_wp_get_term_count_for_object_type_single_object_type() {
+		$term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) );
+		$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
+
+		wp_set_object_terms( $post_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+
+		$term_object = get_term( $term_id, 'category' );
+		$this->assertEquals( 1, $term_object->count );
+
+		wp_remove_object_terms( $post_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+	}
+
+	/**
+	 * @ticket 38280
+	 */
+	public function test_wp_get_term_count_for_object_type_multiple_object_types() {
+		register_post_type( 'wptests_cpt' );
+		register_taxonomy_for_object_type( 'category', 'wptests_cpt' );
+
+		$term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) );
+		$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
+		$custom_post_id = self::factory()->post->create( array( 'post_type' => 'wptests_cpt' ) );
+
+		$this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) );
+
+		wp_set_object_terms( $post_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) );
+
+		wp_set_object_terms( $custom_post_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) );
+
+		$term_object = get_term( $term_id, 'category' );
+		$this->assertEquals( 2, $term_object->count );
+
+		wp_remove_object_terms( $custom_post_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) );
+
+		wp_remove_object_terms( $post_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) );
+	}
+
+	/**
+	 * @ticket 38280
+	 */
+	public function test_wp_get_term_count_for_object_type_multiple_object_types_attachment() {
+		register_taxonomy_for_object_type( 'category', 'attachment' );
+
+		$term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) );
+		$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
+		$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg', $post_id );
+
+		$this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) );
+
+		wp_set_object_terms( $post_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) );
+
+		wp_set_object_terms( $attachment_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) );
+
+		$term_object = get_term( $term_id, 'category' );
+		$this->assertEquals( 2, $term_object->count );
+
+		wp_remove_object_terms( $attachment_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) );
+
+		wp_remove_object_terms( $post_id, array( $term_id ), 'category' );
+
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) );
+	}
+
 	public static function maybe_filter_count() {
 		return 'foo';
 	}
