WordPress自定义文章添加分类筛选

/*adding-a-taxonomy-filter-to-admin-list-for-a-custom-post-type*/
function todo_restrict_manage_posts() {
    global $typenow;
    $args=array( 'public' => true, '_builtin' => false,'name' => '' ); 
    $post_types = get_post_types($args);
    if ( in_array($typenow, $post_types) ) {
    $filters = get_object_taxonomies($typenow);
        foreach ($filters as $tax_slug) {
            $tax_obj = get_taxonomy($tax_slug);
            wp_dropdown_categories(array(
              'show_option_all' => __('Show All '.$tax_obj->label ),
                'taxonomy' => $tax_slug,
                'name' => $tax_obj->name,
                'orderby' => 'term_order',
                'selected' => $_GET[$tax_obj->query_var],
                'hierarchical' => $tax_obj->hierarchical,
                'show_count' => false,
                'hide_empty' => true
            ));
        }
    }
}
function todo_convert_restrict($query) {
    global $pagenow;
    global $typenow;
    if ($pagenow=='edit.php') {
        $filters = get_object_taxonomies($typenow);
        foreach ($filters as $tax_slug) {
            $var = &$query->query_vars[$tax_slug];
            if ( isset($var) ) {
                $term = get_term_by('id',$var,$tax_slug);
                $var = $term->slug;
            }
        }
    }
    return $query;
}
add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
add_filter('parse_query','todo_convert_restrict');


/*adding-metabox-filter-to-admin-list-for-a-custom-post-type*/
/*
**proavance used
*/
function wisdom_filter_tracked_plugins()
{
    global $typenow;
    $current_plugin = '';
    if (isset($_GET['slug'])) {
        $current_plugin = $_GET['slug'];
        $_GET['slug'] = "";
    }
    if ($typenow == 'products') {
        $args = array(
            'post_type' => 'product_labels',
            'posts_per_page' => -1,
            'orderby' => 'meta_value_num',
            'meta_key' => '_label_order_key',
            'order' => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'brands_category',
                    'field' => 'slug',
                    'terms' => 'products_brand',
                ),
            ),
        );
        $new_query = new WP_Query($args);
        if ($new_query->have_posts()) {
            ?>
            <select name="slug" id="slug">
                <option value="all" <?php selected('all', $current_plugin); ?>>All</option>
                <?php
                            while ($new_query->have_posts()) {
                                $new_query->the_post();
                                global $post;
                                $label_slug = get_post_meta($post->ID, '_label_slug_key', true);
                                ?>
                    <option value="<?php echo $label_slug; ?>" <?php selected($label_slug, $current_plugin); ?>>
                        <?php echo $label_slug; ?></option><?php
                                                                        }
                                                                    }
                                                                    ?>
            </select>
    <?php }
    }
function wisdom_sort_plugins_by_slug($query)
    {
        global $pagenow;
        // Get the post type
        $post_type = isset($_GET['post_type']) ? $_GET['post_type'] : '';
        if (is_admin() && $pagenow == 'edit.php' && $post_type == 'products' && isset($_GET['slug']) && $_GET['slug'] != 'all') {
            $query->query_vars['meta_key'] = '_brand_value_key';
            $query->query_vars['meta_value'] = $_GET['slug'];
            $query->query_vars['meta_compare'] = '=';
        }
        return $query;
    }
add_action('restrict_manage_posts', 'wisdom_filter_tracked_plugins'); 
add_filter('parse_query', 'wisdom_sort_plugins_by_slug');