我想你可以试试过滤器list_terms_exclusions
, 但我没有测试。
示例:
function fb_list_terms_exclusions($args) {
if ( \'media-upload-popup\' != $where )
return $where;
if ( !current_user_can(\'manage_categories\') )
$where = " AND t.slug NOT IN (\'uncategorized\', \'category-2\')"; // slug of categories
return $where;
}
add_filter( \'list_terms_exclusions\', \'fb_list_terms_exclusions\' );
更新此asnwer的评论:查找帖子类型的示例;首先是一个使用默认变量的小示例,然后是返回值的函数:
/**
* Include my scripts
*
* $pagehook for check, if this the right page
* $post_type for check, if this the right post type
*/
function fb_post_type_script($pagehook) {
global $post_type;
$pages = array( \'edit.php\', \'post.php\', \'post-new.php\' );
if ( in_array( $pagehook, $pages ) && $post_type == \'my_post_type\' ) {
wp_enqueue_script( \'my_script_example_key\', plugins_url( \'js/my_example_script.js\', __FILE__ ), array( \'jquery\', \'my_other_example_script_key\' ), \'1.0.0\' );
}
}
add_action( \'admin_enqueue_scripts\', \'fb_post_type_script\' );
或者使用自定义函数查找帖子类型:
/**
* Return post type
*
* @return string $post_type
*/
private function get_post_type() {
if ( !function_exists(\'get_post_type_object\') )
return NULL;
if ( isset($_GET[\'post\']) )
$post_id = (int) $_GET[\'post\'];
elseif ( isset($_POST[\'post_ID\']) )
$post_id = (int) $_POST[\'post_ID\'];
else
$post_id = 0;
$post = NULL;
$post_type_object = NULL;
$post_type = NULL;
if ( $post_id ) {
$post = get_post($post_id);
if ( $post ) {
$post_type_object = get_post_type_object($post->post_type);
if ( $post_type_object ) {
$post_type = $post->post_type;
$current_screen->post_type = $post->post_type;
$current_screen->id = $current_screen->post_type;
}
}
} elseif ( isset($_POST[\'post_type\']) ) {
$post_type_object = get_post_type_object($_POST[\'post_type\']);
if ( $post_type_object ) {
$post_type = $post_type_object->name;
$current_screen->post_type = $post_type;
$current_screen->id = $current_screen->post_type;
}
} elseif ( isset($_SERVER[\'QUERY_STRING\']) ) {
$post_type = esc_attr( $_SERVER[\'QUERY_STRING\'] );
$post_type = str_replace( \'post_type=\', \'\', $post_type );
}
return $post_type;
}
public function example() {
$post_type = $this->get_post_type();
// FB_POST_TYPE_1 is defined with my post type
if ( FB_POST_TYPE_1 == $post_type ) {
// tue etwas, wenn wahr
}
}