When you register your taxonomy, set the update_count_callback
argument to _update_generic_term_count
. This will count all objects associated to taxonomy terms so you don’t need to use hide_empty
.
$args = array(
// your other taxonomy arguments here
'update_count_callback' => '_update_generic_term_count'
);
register_taxonomy( 'your-taxonomy', array( 'attachment' ), $args );