Skip to content

Instantly share code, notes, and snippets.

@caratage
Created November 23, 2012 19:37
Show Gist options
  • Save caratage/4136991 to your computer and use it in GitHub Desktop.
Save caratage/4136991 to your computer and use it in GitHub Desktop.
Dropdown menus to sort by custom taxonomies
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