Created
November 14, 2012 15:03
-
-
Save mattboon/4072627 to your computer and use it in GitHub Desktop.
WordPress - Add custom taxonomy filters to custom post types
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
<?php | |
// Add category filters to WP custom post types | |
add_action( 'restrict_manage_posts', 'my_add_category_filters' ); | |
function my_add_category_filters() { | |
global $typenow; | |
if( $typenow == "event" ){ | |
$taxonomy = 'event_type'; | |
} | |
elseif( $typenow == "guideline" ){ | |
$taxonomy = 'guideline_type'; | |
} | |
elseif( $typenow == "resource" ){ | |
$taxonomy = 'resource_type'; | |
} | |
if( $typenow != "post" && $typenow != "page" ) { | |
$filters = array($taxonomy); | |
foreach ($filters as $tax_slug) { | |
$tax_obj = get_taxonomy($tax_slug); | |
$tax_name = $tax_obj->labels->name; | |
$terms = get_terms($tax_slug); | |
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>"; | |
echo "<option value=''>View all categories</option>"; | |
foreach ($terms as $term) { echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>'; } | |
echo "</select>"; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment