Drop "Uncategorized" When Another Category Is Chosen
Removes the default category from a post as soon as it has another category, so posts do not end up filed under both.
Opens WPIDE on
Code
add_action( 'set_object_terms', function ( $object_id, $terms, $tt_ids, $taxonomy ) {
if ( 'category' !== $taxonomy || count( $tt_ids ) < 2 ) {
return;
}
$default = (int) get_option( 'default_category' );
$term = get_term( $default, 'category' );
if ( $term && ! is_wp_error( $term ) && in_array( (int) $term->term_taxonomy_id, array_map( 'intval', $tt_ids ), true ) ) {
wp_remove_object_terms( $object_id, $default, 'category' );
}
}, 10, 4 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.