因此,我尝试了Pawan的答案,虽然它确实有效,但它将过滤器应用到了所有地方,而不是我想要的自定义帖子类型。因此,我做了一个小小的修改,应该可以使这项工作:
public function filter_exhibitions($post_type, $which) {
$target = \'exhibition\'; // Created this target variable and removed the $_GET stuff
if($post_type == $target) { // Test for correct post type
$values = array(
\'Temporary\' => \'temporary\', // Add your own \'Label\' => \'value\' pairs here
\'Permanent\' => \'permanent\',
\'Online\' => \'online\',
);
?>
<select name="ADMIN_FILTER_FIELD_VALUE">
<option value=""><?php _e(\'All exhibition types\', \'artlytical-media\'); // Change this to the default null value display ?></option>
<?php
$current_v = isset($_GET[\'ADMIN_FILTER_FIELD_VALUE\'])? $_GET[\'ADMIN_FILTER_FIELD_VALUE\']:\'\';
foreach ($values as $label => $value) {
printf(
\'<option value="%s"%s>%s</option>\',
$value,
$value == $current_v? \' selected="selected"\':\'\',
$label
);
}
?>
</select>
<?php
}
}
add_action( \'restrict_manage_posts\', \'filter_exhibitions\' );
function exhibition_posts_filter($query){
global $pagenow;
if(is_admin()) {
$type = $query->query[\'post_type\'];
$target = \'exhibition\';
if($type == $target &&
$pagenow == \'edit.php\' &&
isset($_GET[\'ADMIN_FILTER_FIELD_VALUE\']) &&
$_GET[\'ADMIN_FILTER_FIELD_VALUE\'] != \'\') {
$query->query_vars[\'meta_key\'] = \'exhibition_type\'; // Change to meta key created by acf
$query->query_vars[\'meta_value\'] = $_GET[\'ADMIN_FILTER_FIELD_VALUE\'];
}
}
}
add_filter( \'parse_query\', \'wpse45436_posts_filter\' );