Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to delete some postmeta when deleting taxonomy terms from the backend. Before deleting the term, I need to retrieve the postmeta's meka_key and the term id that is going to be deleted. Use the term id to perform some tasks and then delete the postmeta and term.

I already tried using the action hook set_object_terms however, I cannot retrieve the term id when deleting the term because it returns an empty array. So, I would need to somehow get the term id before it gets deleted.

How can I retrieve the term id before deleting it?

add_action( 'set_object_terms', function( $object_id, $terms, $tt_ids, $taxonomy ){

//$terms is returning the term's name (when adding an object id), 
//it's returning empty when deleting the term.
//I need to have a valid $terms when deleting it, where can I get it?
$user_name = $terms;
$meta_key = '_favorite_relation_added_' . $user_name;

  // Customize post type in next line according to your needs. I used 'category' as example
  if ( $taxonomy === 'favorite' ) {
    $post = get_post( $object_id );    
    if ( empty( $post ) ) return;

    // Customize post type in next line according to your needs. I used 'post' as example
    if ( $post->post_type !== 'post' ) return;

    // let's see if the post has some terms of this category,
    // because the hoook is fired also when terms are removed 
    $has_terms = get_the_terms( $object_id, $taxonomy );

    // here we see if the post already has the custom field
    $has = get_post_meta( $post->ID, $meta_key, true );

    if ( ! $has && ! empty( $has_terms ) ) {
      // if the post has not the custom field but has some terms,
      // let's add the custom field setting it to current timestamp     
      update_post_meta( $post->ID, $meta_key, time() );

    } elseif ( $has && empty( $has_terms ) ) {
      // on the countrary if the post already has the custom field but has not terms
      // (it means terms are all removed from post) remove the custom fields    
      delete_post_meta( $post->ID, $meta_key );
    }
  }   
}, 10, 4);
share|improve this question

2 Answers 2

Actually, you can use multiple actions, at least this is what at the end of the wp_delete_term function, which runs when you click Delete on a taxonomy term:

do_action( 'delete_term_taxonomy', $tt_id );
do_action( 'deleted_term_taxonomy', $tt_id );
do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );
do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term );

The last one my be the most useful, you can create an add action like this:

add_action( "delete_favorite" ...
share|improve this answer
    
I just tried that last one but I am getting the same problem as with 'delete_term' I cannot use the function get_term_by because it is returning NULL –  Gixty Mar 19 at 20:35
    
I keep getting NULL or bool(false). I checked and the term is there, so why am I getting null or false? –  Gixty Mar 19 at 21:01

The hook that is running before deleting the term is 'delete_term_taxonomy' And it is the hook needed in order to use the term_id before it gets deleted.

Now, we can proceed to retrieve the term object and delete the postmeta related to the term being deleted.

The code is as follows:

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

 
discard

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.