Index: src/wp-includes/taxonomy.php
===================================================================
--- src/wp-includes/taxonomy.php	(revision 43341)
+++ src/wp-includes/taxonomy.php	(working copy)
@@ -3091,6 +3091,65 @@
 	return true;
 }
 
+/**
+ * Retrieves the term count for a specific object type.
+ *
+ * @since 4.9.0
+ *
+ * @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 ) ) {
+		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 );
+
+	/*
+	 * If the object type has been counted, and other counts exist in meta, we can
+	 * assume this term has 0 relationships for this object type.
+	 */
+	if ( in_array( $object_type, $counted_object_types, true ) ) {
+		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 );
+	}
+}
+
 //
 // Cache
 //
@@ -3634,13 +3693,52 @@
 	foreach ( (array) $terms as $term ) {
 		$count = 0;
 
+		// Remove previous counts to prevent stale data if an object type is removed from a taxonomy.
+		$counted_object_types = (array) get_term_meta( $term, '_wp_counted_object_types', true );
+
+		foreach ( $counted_object_types as $o_type ) {
+			delete_term_meta( $term, '_wp_object_count_' . $o_type );
+		}
+
+		delete_term_meta( $term, '_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, $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 ) );
+			$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 ) );
+
+			$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, '_wp_object_count_' . $type, (int) $term_count_meta[ $type ] );
+				}
+			}
+
+			update_term_meta( $term, '_wp_counted_object_types', $object_types );
+		} else {
+			foreach ( $object_types as $type ) {
+				delete_term_meta( $term, '_wp_object_count_' . $type );
+			}
+
+			delete_term_meta( $term, '_wp_counted_object_types' );
 		}
 
 		/** This action is documented in wp-includes/taxonomy.php */
Index: tests/phpunit/tests/term/getTerms.php
===================================================================
--- tests/phpunit/tests/term/getTerms.php	(revision 43341)
+++ tests/phpunit/tests/term/getTerms.php	(working copy)
@@ -2776,6 +2776,110 @@
 		$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' ) );
+        $this->assertFalse( (bool) get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+
+		$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->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' ) );
+        $this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+
+		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' ) );
+		$this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+
+		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', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+
+		$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' ) );
+        $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+
+		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' ) );
+        $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+    }
+
+	/**
+	 * @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' ) );
+        $this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+
+		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' ) );
+        $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+
+		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' ) );
+        $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+
+		$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' ) );
+        $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
+	}
+
 	public static function maybe_filter_count() {
 		return 'foo';
 	}
