Generate List of Taxonomy IDs NOT Used in a Custom Post Type

In this example I needed to be able to get a list of taxonomy terms used in a specific post type, with the taxonomy crossing between post types…but I only wanted to return terms used in that specific post type. By default WordPress will return all terms used in ANY post type. So I put together this little function that returns a list of term IDs without any posts associated in the custom post type…so then I can used that as my exclude list when querying the taxonomy.

function cpt_taxonomy_excludes($custom_taxonomy, $post_type) {
     $exclude = array();

     foreach (get_terms($custom_taxonomy) as $term) {
          $posts = get_posts(
               array(
                    'post_type' => $post_type,
                    'tax_query' => array(
                         array(
                              'taxonomy' => $custom_taxonomy,
                              'field' => 'id',
                              'terms' => $term->term_id
                         )
                    )
               )
          );

          if (empty($posts)){
               $exclude[] = $term->term_id;
          }
     }
return $exclude;
}