Hook when category is added to post

I’m trying to do something when a category is added to post and saved. I thought that using the save_post hook would have registered when a category is added to a post, but it doesn’t appear to.

When I edit a post and do nothing but change the categories for the post, I don’t get the save_post hook fired (editing the title, body, etc fires the save_post hook successfully). Is there another way to use add_action/add_filter to detect when a category is added to a post?

1
1

You may want to try:

do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);

You can find it under this Docs and the action is located at wp-includes/taxonomy.php

add_action('set_object_terms','wpse5123_set_object_terms',10,4);

function wpse5123_set_object_terms($object_id, $terms, $tt_ids, $taxonomy){
       if($taxonomy == 'category'){
           echo '<pre>';
           print_r($terms);
           echo '</pre>';
           exit;
       }
}

The code above isn’t tested but I think you get the point.

Leave a Comment