Skip to content

Instantly share code, notes, and snippets.

@michelmany
Created January 10, 2024 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michelmany/f11700ab1c7c50d43dd110e9f2d55fb1 to your computer and use it in GitHub Desktop.
Save michelmany/f11700ab1c7c50d43dd110e9f2d55fb1 to your computer and use it in GitHub Desktop.
WordPress remove_default_category (uncategorized)
function remove_default_category($ID, $post) {
//get all categories for the post
$categories = wp_get_object_terms($ID, 'category');
//if there is more than one category set, check to see if one of them is the default
if (count($categories) > 1) {
foreach ($categories as $key => $category) {
//if category is the default, then remove it
if ( $category->name === "Uncategorized") {
wp_remove_object_terms($ID, 'uncategorized', 'category');
}
}
}
}
//hook in to the publsh_post action to run when a post is published
add_action('publish_post', 'remove_default_category', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment