I have a function that whenever i add a new post in the custom post type "artists" it adds a new term with the same name in the custom taxonomy "artists".
function add_artist_category_automatically($post_ID) {
global $wpdb;
if(!has_term('','byartist',$post_ID)){
$cat = get_the_title($post_ID);
wp_set_object_terms($post_ID, $cat, 'byartist');
wp_set_object_terms($post_ID, NULL, 'byartist' );
}
}
add_action('publish_artists', 'add_artist_category_automatically');
Now i want a function that whenever i trash a post from the "artists" custom post to delete the term with the same slug that was created with the auto add function. I haven't managed to make this work:
function delete_artist_term_automatically($post_ID) {
global $wpdb;
$catigoria = get_the_title($post_ID);
$theterm = term_exists($catigoria, 'byartist');
wp_delete_term($theterm->term_id, 'byartist');
}
add_action('trashed_artists', 'delete_artist_term_automatically');
UPDATE: I use get_term_by slug now after getting the post_name and not title. But it doesn't seem to work too. I'm not sure if that's the right way to get the term_id. I also removed $wpdb.
function delete_artist_term_automatically($post_ID) {
$cat = get_post($post_ID);
$termslug = $cat->post_name;
$theterm = get_term_by('slug', $termslug, 'byartist');
wp_delete_term($theterm->term_id, 'byartist');
}
add_action('deleted_artists', 'delete_artist_term_automatically');