I'm running a function that hooks into wp_trash_post and that should delete a custom taxonomy term that is the match of a custom post.
The term description equals the ID of the custom post.
function trash_custom_terms($post_id) {
if ( 'custom_post' != get_post_type($post_id)) {
return;
}
$this_post_ID = get_post( $post_id )->ID;
$existing_terms = get_terms('', array(
'hide_empty' => false
)
);
foreach($existing_terms as $term) {
if ($term->description == $this_post_ID) {
if (wp_delete_term($term->term_id, 'custom_taxonomy')){
write_log('should be deleted');
}else{
write_log('isnt deleted');
}
}
}
}
add_action('wp_trash_post', 'trash_custom_terms');
As can be seen in my code I am using a function to write to the WP debug log.
When the code runs and the matching custom post is deleted, the ID taxonomy term increments by 1! It is as if it is deleted then replaced by an identical term.
I cant figure out what is going on and have checked and double checked that the vars are correct, and they are.
Could anybody please shed some light as to why this may be?
Cheers
Noel