Created
February 17, 2012 11:21
-
-
Save benhuson/1852779 to your computer and use it in GitHub Desktop.
Framework for adding taxonomy filter and column to WordPress manage post admin pages.
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 | |
class Post_Taxonomy_Columns_And_Filter { | |
/** | |
* Constructor | |
*/ | |
function Post_Taxonomy_Columns_And_Filter() { | |
add_filter( 'manage_edit-mycustomposttype_columns', array( $this, 'manage_mycustomposttype_columns' ) ); | |
add_action( 'manage_mycustomposttype_posts_custom_column', array( $this, 'show_mycustomposttype_columns' ) ); | |
add_action( 'restrict_manage_posts', array( $this, 'admin_posts_tax_filter_menu' ) ); | |
add_filter( 'request', array( $this, 'admin_posts_tax_filter_query' ) ); | |
} | |
/** | |
* Add a collection filter menu to the custom post type overview admin page | |
*/ | |
function admin_posts_tax_filter_menu() { | |
global $typenow, $wp_query; | |
if ( $typenow == 'mycustomposttype' ) { | |
$taxonomy = 'mycustomtaxonomy'; | |
$category_taxonomy = get_taxonomy( $taxonomy ); | |
$category_term = get_term_by( 'slug', $wp_query->query['mycustomtaxonomy'], $taxonomy ); | |
wp_dropdown_categories(array( | |
'show_option_all' => __( "Show All {$category_taxonomy->label}" ), | |
'taxonomy' => $taxonomy, | |
'name' => $taxonomy, | |
'orderby' => 'name', | |
'selected' => $category_term->term_id, | |
'hierarchical' => true, | |
'depth' => 3, | |
'show_count' => true, // Show # listings in parens | |
'hide_empty' => false, // Don't show businesses w/o listings | |
)); | |
} | |
} | |
/** | |
* Filter custom post type overview admin page based on collection menu | |
*/ | |
function admin_posts_tax_filter_query( $request ) { | |
global $typenow; | |
if ( is_admin() && $typenow == 'mycustomposttype' ) { | |
$term = get_term( $request['mycustomtaxonomy'], 'mycustomtaxonomy'); | |
$request['mycustomtaxonomy'] = $term->slug; | |
} | |
return $request; | |
} | |
/** | |
* Add custom taxonomy column to manage custom post type page | |
*/ | |
function manage_mycustomposttype_columns( $columns ) { | |
$new_columns = array(); | |
foreach ( $columns as $key => $val ) { | |
if ( $key == 'date' ) | |
$new_columns['mycustomtaxonomy_col'] = __( 'My Custom Taxonomy' ); | |
$new_columns[$key] = $val; | |
} | |
return $new_columns; | |
} | |
/** | |
* Show custom taxonomy column on manage custom post type page | |
*/ | |
function show_mycustomposttype_columns( $name ) { | |
global $post; | |
switch ( $name ) { | |
case 'mycustomtaxonomy_col': | |
echo get_the_term_list( $post->ID, 'mycustomtaxonomy', '', ', ', '' ); | |
break; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment