Changed $term to $term_object (to get ID later), added apply_filters, fixed to work for all post types