我试图用分类法隐藏附件。我想在wp admin的帖子页面中隐藏它们。我不知道如何从ajax\\u query\\u attachments\\u args检测我所在的页面。我尝试使用pre\\u get\\u posts,但它似乎不会触发ajax\\u query\\u attachments\\u args。
add_filter(\'ajax_query_attachments_args\', \'hide_attachment_with_taxonomy\', 50, 1);
function hide_attachment_with_taxonomy( $query = array() ) {
global $current_screen;
if ( ! $current_screen ) {
return $query; // It always return this value. $current_screen or get_current_screen() is always undefined
}
if($current_screen->post_type == \'post\'){
$query[\'tax_query\'] = array(
array(
\'taxonomy\' => \'category\',
\'operator\' => \'NOT EXISTS\'
),
);
return $query;
}
}
SO网友:Nomadic Julien
答案是wp\\u get\\u referer()。此函数在ajax\\u query\\u attachments\\u args中工作。然后,我可以将get\\u post\\u type与url参数中的post id一起使用
$referer = parse_url(wp_get_referer());
parse_str($referer[\'query\'], $params);
if (isset($params[\'post\'])){
$post_type = get_post_type($params[\'post\']);
} else if (strpos($referer[\'path\'], \'post-new.php\') !== false && !isset($params[\'post_type\'])){
$post_type = \'post\';
} else {
$post_type = \'\';
}
if ( $post_type == \'post\' ) {
$query[\'tax_query\'] = array(
array(
\'taxonomy\' => \'category\',
\'operator\' => \'NOT EXISTS\'
),
);
}