Last active
August 29, 2015 14:05
-
-
Save PurpleHippoDesign/5313b825564971d4d57a to your computer and use it in GitHub Desktop.
Adds category filter for custom post types
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
<?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