Index: src/wp-includes/taxonomy.php
===================================================================
--- src/wp-includes/taxonomy.php	(revision 43347)
+++ src/wp-includes/taxonomy.php	(working copy)
@@ -3091,6 +3091,72 @@
 	return true;
 }
 
+/**
+ * Retrieves the term count for a specific object type.
+ *
+ * @since 5.0.0
+ *
+ * @param int    $term_id     Term ID.
+ * @param string $taxonomy    Taxonomy name.
+ * @param string $object_type Object type.
+ *
+ * @return WP_Error|int WP_Error on failure, 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.' ),
+			array(
+				'taxonomy' => $taxonomy,
+			)
+		);
+	}
+
+	if ( ! is_object_in_taxonomy( $object_type, $taxonomy ) ) {
+		return new WP_Error(
+			'invalid_object_type',
+			__( 'Object type is not in taxonomy.' ),
+			array(
+				'object_type' => $object_type,
+				'taxonomy'    => $taxonomy,
+			)
+		);
+	}
+
+	$term = get_term( $term_id, $taxonomy );
+
+	if ( is_wp_error( $term ) ) {
+		return $term;
+	}
+
+	if ( 0 === $term->count ) {
+		return 0;
+	}
+
+	$taxonomy_object = get_taxonomy( $taxonomy );
+
+	if ( 1 >= count( $taxonomy_object->object_type ) ) {
+		return $term->count;
+	}
+
+	$term_object_count = get_term_meta( $term_id, '_wp_object_count_' . $object_type, true );
+	if ( $term_object_count ) {
+		return (int) $term_object_count;
+	}
+
+	$counted_object_types = (array) get_term_meta( $term_id, '_wp_counted_object_types', true );
+
+	// When the object type is marked counted and no meta key exists, the count is 0.
+	if ( in_array( $object_type, $counted_object_types, true ) ) {
+		return 0;
+	}
+
+	// Term has not been counted for the object type. Calculate and fetch count.
+	wp_update_term_count_now( array( $term->term_taxonomy_id ), $taxonomy );
+	return wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type );
+}
+
 //
 // Cache
 //
@@ -3605,6 +3671,7 @@
  *
  * @access private
  * @since 2.3.0
+ * @since 5.0.0 Store term counts on a per object type basis in meta.
  *
  * @global wpdb $wpdb WordPress database abstraction object.
  *
@@ -3628,27 +3695,64 @@
 	}
 
 	if ( $object_types ) {
-		$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
+		$object_types = array_filter( $object_types, 'post_type_exists' );
 	}
 
-	foreach ( (array) $terms as $term ) {
+	foreach ( (array) $terms as $tt_id ) {
 		$count = 0;
 
+		$term = get_term_by( 'term_taxonomy_id', $tt_id );
+
+		// Remove previous counts to prevent stale data if an object type is removed from a taxonomy.
+		$counted_object_types = (array) get_term_meta( $term->term_id, '_wp_counted_object_types', true );
+
+		foreach ( $counted_object_types as $o_type ) {
+			delete_term_meta( $term->term_id, '_wp_object_count_' . $o_type );
+		}
+
+		delete_term_meta( $term->term_id, '_wp_counted_object_types' );
+
+		$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, $tt_id ) );
+
+				$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 ) );
+			$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", $tt_id ) );
+
+			$count += $attachment_count;
+
+			$term_count_meta['attachment'] = $attachment_count;
+
+			// Re-add attachment so the meta gets saved below.
+			$object_types[] = 'attachment';
 		}
 
-		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 ) );
+		// 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 ] ) ) {
+					update_term_meta( $term->term_id, '_wp_object_count_' . $type, (int) $term_count_meta[ $type ] );
+				}
+			}
+
+			update_term_meta( $term->term_id, '_wp_counted_object_types', $object_types );
 		}
 
 		/** 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 ) );
+		do_action( 'edit_term_taxonomy', $tt_id, $taxonomy->name );
+		$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $tt_id ) );
 
 		/** This action is documented in wp-includes/taxonomy.php */
-		do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
+		do_action( 'edited_term_taxonomy', $tt_id, $taxonomy->name );
 	}
 }
 
Index: tests/phpunit/tests/term/wpGetObjectTerms.php
===================================================================
--- tests/phpunit/tests/term/wpGetObjectTerms.php	(revision 43347)
+++ tests/phpunit/tests/term/wpGetObjectTerms.php	(working copy)
@@ -599,6 +599,14 @@
 		$p = self::factory()->post->create();
 		wp_set_object_terms( $p, $terms, 'wptests_tax' );
 
+		/**
+		 * `wp_set_object_terms()` populates the cache, but we need it empty to verify
+		 * behavior of 'update_term_meta_cache'.
+		 */
+		foreach ( $terms as $t ) {
+			wp_cache_delete( $t, 'term_meta' );
+		}
+
 		$found = wp_get_object_terms(
 			$p, 'wptests_tax', array(
 				'update_term_meta_cache' => false,
Index: tests/phpunit/tests/term/wpGetTermCountForObjectType.php
===================================================================
--- tests/phpunit/tests/term/wpGetTermCountForObjectType.php	(nonexistent)
+++ tests/phpunit/tests/term/wpGetTermCountForObjectType.php	(working copy)
@@ -0,0 +1,189 @@
+<?php
+
+/**
+ * Class Tests_Term_wpGetTermCountForObjectType
+ *
+ * @group taxonomy
+ * @ticket 38280
+ */
+class Tests_Term_wpGetTermCountForObjectType extends WP_UnitTestCase {
+
+	/**
+	 * Test when a taxonomy is invalid.
+	 */
+	public function test_wp_get_term_count_for_object_type_invalid_taxonomy() {
+		$actual = wp_get_term_count_for_object_type( 0, 'does-not-exist', 'post' );
+
+		$this->assertWPError( $actual );
+		$this->assertSame( 'invalid_taxonomy', $actual->get_error_code() );
+	}
+
+	/**
+	 * Test when an object type is not in a taxonomy.
+	 */
+	public function test_wp_get_term_count_for_object_type_object_not_in_taxonomy() {
+		$this->assertWPError( wp_get_term_count_for_object_type( 0, 'category', 'page' ) );
+	}
+
+	/**
+	 * Test when an invalid term is passed.
+	 */
+	public function test_wp_get_term_count_for_object_type_invalid_term() {
+		$actual = wp_get_term_count_for_object_type( 0, 'category', 'post' );
+
+		$this->assertWPError( $actual );
+		$this->assertSame( 'invalid_term', $actual->get_error_code() );
+	}
+
+	/**
+	 * Test when a taxonomy belongs to a single object type. No term meta should be
+	 * stored and the count property should be used.
+	 */
+	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',
+				'post_category' => array(
+					$term_id,
+				),
+			)
+		);
+
+		$this->assertFalse( (bool) get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$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' ) );
+
+		$term_object = get_term( $term_id, 'category' );
+		$this->assertEquals( 0, $term_object->count );
+	}
+
+	/**
+	 * Test when a taxonomy belongs to multiple object types.
+	 */
+	public function test_wp_get_term_count_for_object_type_multiple_object_types() {
+		register_post_type( 'wptests_cpt' );
+		register_taxonomy(
+			'wptests_tax',
+			array(
+				'post',
+				'wptests_cpt',
+			)
+		);
+
+		$term_id        = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+		$post_id        = self::factory()->post->create( array( 'post_type' => 'post' ) );
+		$custom_post_id = self::factory()->post->create( array( 'post_type' => 'wptests_cpt' ) );
+
+		// When a term has no relationships, no meta should be stored.
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
+
+		wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
+
+		// Term has relationships, meta should be stored caching types counted and counts for each type > 0.
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
+		$this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
+
+		wp_set_object_terms( $custom_post_id, array( $term_id ), 'wptests_tax' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
+		$this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
+
+		// Total count should be stored in the term's count property.
+		$term_object = get_term( $term_id, 'wptests_tax' );
+		$this->assertEquals( 2, $term_object->count );
+
+		wp_remove_object_terms( $custom_post_id, array( $term_id ), 'wptests_tax' );
+
+		// Object count cache should be removed.
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
+		$this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
+
+		wp_remove_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
+
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
+		$this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
+	}
+
+	/**
+	 * Test when a taxonomy belongs to multiple object types, one of which is attachments.
+	 */
+	public function test_wp_get_term_count_for_object_type_multiple_object_types_attachment() {
+		register_taxonomy(
+			'wptests_tax',
+			array(
+				'post',
+				'attachment',
+			)
+		);
+
+		$term_id       = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+		$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 );
+
+		// When a term has no relationships, no meta should be stored.
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
+
+		wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
+
+		// Term has relationships, meta should be stored caching types counted and counts for each type > 0.
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
+		$this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
+
+		wp_set_object_terms( $attachment_id, array( $term_id ), 'wptests_tax' );
+
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
+		$this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
+
+		// Total count should be stored in the term's count property.
+		$term_object = get_term( $term_id, 'wptests_tax' );
+		$this->assertEquals( 2, $term_object->count );
+
+		wp_remove_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
+
+		// Object count cache should be removed.
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
+		wp_remove_object_terms( $attachment_id, array( $term_id ), 'wptests_tax' );
+
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
+		$this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
+		$this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
+		$this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
+	}
+}
