wp_terms_checklist
early trigger a filter \'wp_terms_checklist_args\'
因此,您可以使用该过滤器添加过滤器,并在第一次运行时自动删除它。
然而,这应该足够了,一旦挂钩是全局变量,为了确保更好地使用更强大的检查,函数中的静态变量是一个简单而好的技巧:
function switch_terms_filter( $_set = NULL ) {
static $set;
if ( ! is_null($_set) ) $set = $_set;
return $set;
}
add_filter( \'wp_terms_checklist_args\', function( $args ) {
// turn the switch ON
switch_terms_filter(1);
add_filter( \'get_terms\', \'your_filter_callback\', 10, 3 );
return $args; // we don\'t want to affect wp_terms_checklist $args
} );
function your_filter_callback( $terms, $taxonomies, $args ) {
// remove filter after 1st run
remove_filter( current_filter(), __FUNCTION__, 10, 3 );
// is the switch ON? If not do nothing.
if ( switch_terms_filter() !== 1 ) return $terms;
switch_terms_filter(0); // turn the switch OFF
// ... filter terms here ...
return $terms;
}
Untested.