WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use the action hooks delete_term and delete_$taxonomy, among other hooks that delete a term, but they are presenting the same issue.

For example, 1234 is an existing term in the favorite custom taxonomy:

add_action( 'delete_favorite', function( $term, $tt_id ) {

    $get_term1 = get_term( $term, 'favorite' );
    $get_term2 = get_term( 1234, 'favorite' );

    $get_term_by1 = get_term_by( 'id', $term, 'favorite' );
    $get_term_by2 = get_term_by( 'id', 1234, 'favorite' );
    $get_term_by3 = get_term_by( 'name', 'term-name', 'favorite' );

    $output = 'get_term1: ' . $get_term1 . ' get_term2: ' . $get_term2 . 
        ' get_term_by1: ' . $get_term_by1 . ' get_term_by2: ' . $get_term_by2 . 
        ' get_term_by3: ' . $get_term_by3;

    var_dump( $output );
    $contents = ob_get_contents();
    error_log( $contents );

}, 10, 2 );

They all return nothing. So, how can I get the term inside these types of functions?

The functions get_term and get_term_by work everywhere else but inside these hooks. What is wrong?

share|improve this question

To use get_term_by we need the term object before it gets deleted. Therefore, we should use the action hook delete_term_taxonomy that runs before the term is deleted.

Now, we can go ahead and perform some tasks such as deleting all the postmeta related to the term being deleted.

The code is:

add_action( 'delete_term_taxonomy', function($tt_id) {

    $taxonomy = 'category';    
    $term = get_term_by('term_taxonomy_id', $tt_id, $taxonomy); 
    $user_name = $term->name;
    $meta_key = "_category_relation_added_" . $user_name;
    delete_post_meta_by_key( $meta_key );

}, 9, 1);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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