如果有人面临同样的问题,下面是一个片段:
// Based on the User Contributed Notes of the Codex
add_action(\'restrict_manage_posts\',\'my_post_type_filter\', 10, 2);
function my_post_type_filter($post_type, $which){
if(\'faq\' !== $post_type){
return; // check the post type
}
$taxonomy_slug = \'faq_cat\'; // subject to change
$taxonomy = get_taxonomy($taxonomy_slug);
$request_attr = \'faq_cat\'; // subject to change, will show up in the url
$selected = \'\';
if ( isset($_REQUEST[$request_attr] ) ) {
$selected = $_REQUEST[$request_attr]; // in case the current page is already filtered
}
wp_dropdown_categories(array(
\'show_option_all\' => __("Show All {$taxonomy->label}"),
\'taxonomy\' => $taxonomy_slug,
\'name\' => $request_attr,
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hierarchical\' => true,
\'depth\' => 1,
\'show_count\' => true, // Show number of post in parent term
\'hide_empty\' => false, // Don\'t show posts w/o terms
\'selected\' => $selected,
));
}
/**
* Filter posts by taxonomy in admin
* @author Mike Hemberger
* @link http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/
*/
add_filter(\'parse_query\', \'tsm_convert_id_to_term_in_query\');
function tsm_convert_id_to_term_in_query($query) {
global $pagenow;
$post_type = \'faq\'; // subject to change
$taxonomy = \'faq_cat\'; // subject to change
$q_vars = &$query->query_vars;
if ( $pagenow == \'edit.php\' && isset($q_vars[\'post_type\']) && $q_vars[\'post_type\'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by(\'id\', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}