Skip to content

Instantly share code, notes, and snippets.

@PurpleHippoDesign
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PurpleHippoDesign/5313b825564971d4d57a to your computer and use it in GitHub Desktop.
Save PurpleHippoDesign/5313b825564971d4d57a to your computer and use it in GitHub Desktop.
Adds category filter for custom post types
<?php // get rid of this tag
// Filter the request to just give posts for the given taxonomy, if applicable.
function taxonomy_filter_restrict_manage_posts() {
global $typenow;
$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' => (isset($_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