从后端删除分类术语的操作钩子是什么?以及如何在删除术语ID之前将其退役?

时间:2014-03-19 作者:Gixty

我需要删除一些postmeta 从后端删除分类术语时。删除之前term, 我需要找回Posteta的meka_key 以及术语id 这将被删除。使用术语id 执行某些任务,然后删除postmetaterm.

我已经试过用动作钩了set_object_terms 但是,我无法检索术语id 删除时term 因为它返回一个空数组。所以,我需要以某种方式得到这个术语id 在它被删除之前。

如何在删除术语id之前检索它?

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 个回复
SO网友:passatgt

实际上,您可以使用多个操作,至少这是wp\\u delete\\u term函数末尾的操作,该函数在单击分类术语上的delete时运行:

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 );
最后一个可能最有用,您可以创建如下添加操作:

add_action( "delete_favorite" ...

SO网友:Gixty

删除术语之前运行的挂钩是\'delete_term_taxonomy\' 它是在删除之前使用术语\\u id所需的钩子。

现在,我们可以继续检索term 对象并删除postmeta 与正在删除的术语相关。

代码如下:

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

SO网友:cardy

正如@passatgt所述,至少有4个动作需要挂钩。顺便说一句,最有用的是“delete\\u$taxonomy”操作。该操作接受3个参数。要使用所有这些参数,必须显式声明要在回调函数中接收多少参数。可以通过将其指定为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

    }

结束

相关推荐

Content hooks vs User hooks

这与其说是一个有直接答案的问题,不如说是一个理论问题。我一直在处理更新或删除帖子以及更新或删除用户时启动功能的不同操作。对于行动,publish_post 和before_delete_post 对于职位和personal_options_update, edit_user_profile_update 和delete_user 对于用户。通过更新后,您可以访问当前设置的值,同时访问新值,以便在发生任何事情之前进行您认为合适的任何更改。使用用户更新,您只能在设置新信息后才能访问新信息。Is there a