Created
June 12, 2017 09:00
-
-
Save michaelwilhelmsen/88adb0b867a89d3395ab3aba68cbac94 to your computer and use it in GitHub Desktop.
Adds filter select box for custom post types in admin.
Change $typenow to post-type, $taxonomies to the taxonomy-category/slug.
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
| // FILTERING IN ADMIN | |
| function sp_add_taxonomy_filters() { | |
| global $typenow; | |
| // an array of all the taxonomies you want to display. Use the taxonomy name or slug | |
| $taxonomies = array('publikasjoner_category'); | |
| // must set this to the post type you want the filter(s) displayed on | |
| if( $typenow == 'publikasjoner' ){ | |
| foreach ($taxonomies as $tax_slug) { | |
| $tax_obj = get_taxonomy($tax_slug); | |
| $tax_name = $tax_obj->labels->name; | |
| $terms = get_terms($tax_slug); | |
| if(count($terms) > 0) { | |
| echo "<select name='$tax_slug' id='$tax_slug' class='postform'>"; | |
| echo "<option value=''>Show All $tax_name</option>"; | |
| foreach ($terms as $term) { | |
| echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>'; | |
| } | |
| echo "</select>"; | |
| } | |
| } | |
| } | |
| } | |
| add_action( 'restrict_manage_posts', 'sp_add_taxonomy_filters' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment