Skip to content

Instantly share code, notes, and snippets.

@gyrus
Created July 21, 2012 14:35
Show Gist options
  • Save gyrus/3155971 to your computer and use it in GitHub Desktop.
Save gyrus/3155971 to your computer and use it in GitHub Desktop.
WordPress custom post type taxonomy list filters
<?php
/**
* Taxonomy list filters and columns
*
* @todo Test!
* @todo Need replacement function for slt_terms_list()
* @link http://wordpress.stackexchange.com/questions/578/adding-a-taxonomy-filter-to-admin-list-for-a-custom-post-type/3215#3215
*/
add_action( 'restrict_manage_posts', 'pilau_restrict_manage_posts' );
function pilau_restrict_manage_posts() {
// only display these taxonomy filters on desired custom post_type listings
global $typenow;
if ($typenow == 'photos' || $typenow == 'videos') {
// create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
$filters = array('plants', 'animals', 'insects');
foreach ($filters as $tax_slug) {
// retrieve the taxonomy object
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
// retrieve array of term objects per taxonomy
$terms = get_terms($tax_slug);
// output html for taxonomy dropdown filter
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
echo "<option value=''>Show All $tax_name</option>";
foreach ($terms as $term) {
// output each select option line, check against the last $_GET to show the current option selected
echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
}
echo "</select>";
}
}
}
add_filter( "manage_edit-project_columns", "pilau_project_columns" );
add_action( "manage_posts_custom_column", "pilau_custom_post_columns" );
function pilau_project_columns( $columns ) {
$columns[ "projecttype" ] = "Type(s)";
$columns[ "worktype" ] = "Work";
return $columns;
}
function pilau_custom_post_columns( $column ) {
if ( $column == "projecttype" ) {
echo slt_terms_list( "projecttype" );
} else if ( $column == "worktype" ) {
echo slt_terms_list( "worktype" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment