Index: src/wp-includes/taxonomy.php
===================================================================
--- src/wp-includes/taxonomy.php	(revision 39474)
+++ src/wp-includes/taxonomy.php	(working copy)
@@ -2897,6 +2897,36 @@
 	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_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
 //
@@ -3387,14 +3417,34 @@
 
 	foreach ( (array) $terms as $term ) {
 		$count = 0;
+		$term_count_meta = (array) 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 ) );
Index: tests/phpunit/tests/term/getTerms.php
===================================================================
--- tests/phpunit/tests/term/getTerms.php	(revision 39474)
+++ 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,51 @@
 		$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 );
+	}
+
+	/**
+	 * @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' ) );
+
+		$this->assertEquals( array( 'post' => 1, 'wptests_cpt' => 1 ), get_term_meta( $term_id, '_wp_term_counts', true ) );
+
+		$term_object = get_term( $term_id, 'category' );
+		$this->assertEquals( 2, $term_object->count );
+	}
+
 	public static function maybe_filter_count() {
 		return 'foo';
 	}
