Created
February 3, 2014 22:43
-
-
Save seafarer/8793993 to your computer and use it in GitHub Desktop.
Sexy Wordpress admin filter for custom post types with custom taxonomies. Plug and play - no config necessary. Via [stackexchange] (http://wordpress.stackexchange.com/questions/578/adding-a-taxonomy-filter-to-admin-list-for-a-custom-post-type)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function todo_restrict_manage_posts() { | |
| global $typenow; | |
| $args=array( 'public' => true, '_builtin' => false ); | |
| $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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment