I have enabled categories on attachments, and I am trying to implement the logic to automatically handle the "Uncategorized" category, like this:
1) If the attachment has no categories, set it to "Uncategorized"
2) If the attachment has some category besides "Uncategorized", remove "Uncategorized" from the post's category list
I have tackled most of the logic, except that now I dont know how to remove the category from the Attachment. This is my code:
<?php
// Enable categories with attachments
register_taxonomy_for_object_type( 'category', 'attachment' );
if( is_admin() ) {
add_action('add_attachment', 'emt_set_attachment_category');
add_action('edit_attachment', 'emt_set_attachment_category');
function emt_set_attachment_category( $post_ID ) {
$attachmentCategories = wp_get_object_terms( $post_ID, 'category' );
// if attachment has many categories, remove the default category
if (count($attachmentCategories) > 1) {
foreach ($attachmentCategories as $key => $category)
if ($category->name == "Uncategorized")
// How to delete it??
return;
}
// if attachment already have categories, stop here
if (count($attachmentCategories) == 1)
return;
// if attachment has no categories, set default category
wp_set_post_categories( $post_ID, array( get_option('default_category') ) );
}
}