1

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);

3 Answers 3

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" ...
2
  • 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, 2014 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, 2014 at 21:01
1

As @passatgt stated there are at least 4 actions to hook. Btw the most useful is "delete_$taxonomy" action. The action accepts 3 parameters. To use all of them you have to explicit declare how many parameters you want to receive in the callback function. You can do this by specifing it as 3rd parameter of add_action($hook, $function_to_add, $priority, **$accepted_args** );.

add_action( "delete_$my_taxonomy",'wpse_138351_delete_taxonomy_meta', 10,3 );

/**
* @$term_id integer id of the term is gonna be deleted
* @$term_taxonomy_id integer
* @$deleted_term object term object (object of class stdClass)
**/    
wpse_138351_delete_taxonomy_meta($term_id, $term_taxonomy_id, $deleted_term )
    {
    
    // here you can use $deleted_term properties to do whatever you want

    }
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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