创建一个函数,该函数接收已在自定义帖子类型中更改的分类术语

时间:2012-08-13 作者:JamesG

我一直在调查行动和挂钩等。。。来自wordpress,但似乎做不到一件非常简单的事情。我只想在每次更新帖子时执行一个函数,在该函数中,我想要保存时更改的术语的名称。因此,可能是某种前后比较的术语??

谁能给我一个进去的方向的提示吗?我从以下内容开始:

add_action(\'save_post\', \'myFunction\');
但我似乎无法将分类前后的术语传递给它进行比较,以查看刚刚选择了哪些。。如果这有意义的话。

谢谢:)

3 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

WordPress更新帖子的分类术语后,会触发以下操作:

set_object_terms
(see source) 其中传递6个变量:

分配给帖子(或根据5添加)的对象ID(可能是其slug或term ID)

  • 上述分类法术语ID(非术语ID)
  • 分类法追加。如果为true,则将2中的术语添加到现有术语中。如果为false(默认),则替换现有条款
  • 更新前的帖子术语(作为分类术语ID)
    1. 问题1

      很遗憾,无法知道该对象是否引用了帖子或其他内容(如用户)。这可能是一种set_object_terms 只有当你能确定它下次被解雇时,它才是指一篇帖子。

      问题2

      传递的ID是分类术语ID,而不是术语ID-大多数WordPress函数都使用术语ID。因此,您可能需要从分类术语ID中获取术语ID(和分类),才能执行任何有用的操作。一个潜在的解决方法是,因为我们知道分类法,列出所有术语并执行一个简单的foreach 循环并过滤它们。Better suggestions are welcome

      以下内容未经测试

       add_action(\'set_object_terms\',\'wpse61678_terms_changed\',10,6);
      
       function wpse61678_terms_changed($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids){
      
            //Note problem 1 - we might not necessarily know what $object_id refers to. 
      
           //Added terms are specified terms, that did not already exist
           $added_tt_ids = array_diff($tt_ids, $old_tt_ids);
      
           if( $append ){
               //If appending terms - nothing was removed
               $removed_tt_ids = array();
           }else{
               //Removed terms will be old terms, that were not specified in $tt_ids
               $removed_tt_ids = array_diff($old_tt_ids, $tt_ids);
           }
      
          /*Note problem 2, we would preferably like the term objects / IDs, 
            Currently we have the taxonomy term IDs*/
      
          //Get all terms
          $all_terms = get_terms( $taxonomy, array(\'hide_empty\'=>0));
      
          $removed_terms =array()
          $added_terms =array();
          foreach( $all_terms as $term ){
               $tt_id = (int) $term->term_taxonomy_id;
               if( in_array( $tt_id, $removed_tt_ids) ){
                   $removed_terms[] = $term;
               }elseif( in_array( $tt_id, $added_tt_ids) ){
                   $added_terms[] = $term;
               }
          }
      
          //$added_terms contains added term objects
          //$removed_terms contains removed term objects
      
      
       }
      

    SO网友:Adam

    如果有potential 使用的问题set_object_terms Stephen指出的钩子,可能钩住pre_post_update 然后接着是wp_insert_post就足够了,

    add_action(\'pre_post_update\',\'get_terms_before_update\');
    function get_terms_before_update(){
        $terms_before = get_the_terms( $id, $taxonomy );
        //do something else if required...
        return $terms_before; 
    }
    
    add_action(\'wp_insert_post\',\'get_terms_after_update\');
        function get_terms_after_update(){
        //pass in $terms_before from get_terms_before_update() function
        //compare terms
    }
    

    SO网友:Jason Wisely

    这是Stephen答案的更新,基于新的get\\u terms\\u by function,并假设我们知道需要什么样的分类法。

    add_action(\'set_object_terms\',\'save_terms\',10,6);
    
    function save_terms($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {
    
        //Assuming we only want terms from specific taxonomy
        //If not simply remove this conditional
        $our_taxonomy = \'categories\';
        if($taxonomy == $our_taxonomy){
            //Added terms are specified terms, that did not already exist
            $added_tt_ids = array_diff($tt_ids, $old_tt_ids);
    
            if ($append) {
                //If appending terms - nothing was removed
                $removed_tt_ids = array();
            } else {
                //Removed terms will be old terms, that were not specified in $tt_ids
                $removed_tt_ids = array_diff($old_tt_ids, $tt_ids);
            }
    
            /* Note problem 2 by Stephen, we would preferably like the term objects / IDs, 
              Currently we have the taxonomy term IDs */
    
            foreach($added_tt_ids as $term){
                $added_terms[] = get_term_by( \'term_taxonomy_id\',$term,$taxonomy);
            }
    
            foreach($removed_tt_ids as $term){
                $removed_terms[] = get_term_by( \'term_taxonomy_id\',$term,$taxonomy);
            }
    
            //$added_terms contains added term objects
            //$removed_terms contains removed term objects
        }
    }
    

    结束

    相关推荐

    WP_GET_OBJECT_TERMS()以获取附加到当前查询中所有帖子的所有术语的列表

    如何使用wp\\u get\\u object\\u terms()获取当前查询中附加到所有帖子的所有术语的列表?例如,对于当前查询,我想从所有查询的帖子中包含的“Alfa”分类中获取一个术语数组。wp_get_object_terms($wp_query, \'alfa\'); 但似乎只返回数组中的一个项目。。。我这样做是为了构建一个数组来交叉检查导航菜单中的一个分类法和另一个分类法,目前正在使用以下代码进行交叉检查,但我认为一定有更好的方法。请帮忙!谢谢$queried_terms = ar