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;
    $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');
  • May I point out that this can be broken by changing the title of a post, then saving. Use the post slug instead of the title – Tom J Nowell Dec 5 '12 at 14:00
  • Thanks Tom, i didn't thought about that and in fact i had already tried with the slug. – drivebass Dec 5 '12 at 16:39

do you know where is your code fails? Is the hook runs properly? One thing for sure, term_exists() returns the ID not an object. Also $wpdb is not needed at all.

wp_delete_term($theterm, 'byartist');
  • 28inch thanks for the suggestions. I updated my question with the latest changes, still it doesn't work. The publish_post function is working when adding an artist i don't know about the trashed_post. – drivebass Dec 5 '12 at 16:45
  • You still using '$theterm->term_id' in your code. term_exists() returns the id NOT an object. as I said try to go through your code and see which part is failing. Use var_dump or echo in every step to make sure what is happening. – 28inch Dec 5 '12 at 16:58
  • Thanks for the comment. If you see me updated function I don't use term_exists() now, i have changed it to get_term_by() and according to wp codex this returns an array? Please let me know if this is wrong. – drivebass Dec 5 '12 at 17:53
  • Sorry, I missed that! What I`m trying to say here is that you should be able to debug this stuff on your own without any problem using, var_dump(), echo() or something more advanced debug tool step by step. Is your hook fires at all (delete_artist_term_automatically)? With other words trashing the artist is actualy calls your function? If the answer is yes $theterm->term_id is gving you an actual id? ...and so on. – 28inch Dec 6 '12 at 12:08
  • 1
    So... the hook first. Since the 'trash action' redirects, the normal console log and vardump is out of the question. You either use the built in logging system or you just simply set a cookie inside the hook something like this: setcookie("hook_running","true",time()+7200); and check it inside the Chrome inspector (f12/resources/cookies). If the hook is ok and still not working just change it to 'admin_init' and pass a $post_id manually and you can use var_dump($varname) manually. You will see the values injected at the top of the admin page. – 28inch Dec 6 '12 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.