Created
January 10, 2024 02:11
-
-
Save michelmany/f11700ab1c7c50d43dd110e9f2d55fb1 to your computer and use it in GitHub Desktop.
WordPress remove_default_category (uncategorized)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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