Created
November 23, 2012 19:37
-
-
Save caratage/4136991 to your computer and use it in GitHub Desktop.
Dropdown menus to sort by custom taxonomies
This file contains 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 taxonomy_filter_restrict_manage_posts() { | |
global $typenow; | |
// If you only want this to work for your specific post type, | |
// check for that $type here and then return. | |
// This function, if unmodified, will add the dropdown for each | |
// post type / taxonomy combination. | |
$post_types = get_post_types( array( '_builtin' => false ) ); | |
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' => 'name', | |
'selected' => $_GET[$tax_slug], | |
'hierarchical' => $tax_obj->hierarchical, | |
'show_count' => false, | |
'hide_empty' => true | |
) ); | |
} | |
} | |
} | |
add_action( 'restrict_manage_posts', 'taxonomy_filter_restrict_manage_posts' ); | |
function taxonomy_filter_post_type_request( $query ) { | |
global $pagenow, $typenow; | |
if ( 'edit.php' == $pagenow ) { | |
$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; | |
} | |
} | |
} | |
} | |
add_filter( 'parse_query', 'taxonomy_filter_post_type_request' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment