我一直在调查行动和挂钩等。。。来自wordpress,但似乎做不到一件非常简单的事情。我只想在每次更新帖子时执行一个函数,在该函数中,我想要保存时更改的术语的名称。因此,可能是某种前后比较的术语??
谁能给我一个进去的方向的提示吗?我从以下内容开始:
add_action(\'save_post\', \'myFunction\');
但我似乎无法将分类前后的术语传递给它进行比较,以查看刚刚选择了哪些。。如果这有意义的话。谢谢:)
我一直在调查行动和挂钩等。。。来自wordpress,但似乎做不到一件非常简单的事情。我只想在每次更新帖子时执行一个函数,在该函数中,我想要保存时更改的术语的名称。因此,可能是某种前后比较的术语??
谁能给我一个进去的方向的提示吗?我从以下内容开始:
add_action(\'save_post\', \'myFunction\');
但我似乎无法将分类前后的术语传递给它进行比较,以查看刚刚选择了哪些。。如果这有意义的话。谢谢:)
WordPress更新帖子的分类术语后,会触发以下操作:
set_object_terms
(see source) 其中传递6个变量:分配给帖子(或根据5添加)的对象ID(可能是其slug或term ID)
set_object_terms
只有当你能确定它下次被解雇时,它才是指一篇帖子。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
}
如果有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
}
这是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\\u get\\u object\\u terms()获取当前查询中附加到所有帖子的所有术语的列表?例如,对于当前查询,我想从所有查询的帖子中包含的“Alfa”分类中获取一个术语数组。wp_get_object_terms($wp_query, \'alfa\'); 但似乎只返回数组中的一个项目。。。我这样做是为了构建一个数组来交叉检查导航菜单中的一个分类法和另一个分类法,目前正在使用以下代码进行交叉检查,但我认为一定有更好的方法。请帮忙!谢谢$queried_terms = ar