Last active
May 4, 2017 05:53
-
-
Save kevinlisota/75b3264f7b91e88540d8 to your computer and use it in GitHub Desktop.
Remove default WordPress category at time of publishing when another category is present
This file contains 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
//remove default category (uncategorized) when another category has been set | |
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
not working for me :(