-
-
Save chodorowicz/816511 to your computer and use it in GitHub Desktop.
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 | |
/* | |
Description: Adds a taxonomy filter in the admin list page for a custom post type. | |
Written for: http://wordpress.stackexchange.com/posts/582/ | |
By: Mike Schinkel - http://mikeschinkel.com/custom-workpress-plugins | |
Instructions: Put this code in your theme's functions.php file or inside your own plugin. Edit to suite your post types and taxonomies. Hope this helps... | |
*/ | |
// | |
add_filter('manage_{custom_post_type_name}_posts_columns', 'add_custom_taxonomy_column'); | |
function add_custom_taxonomy_column( $posts_columns ) { | |
if (!isset($posts_columns['author'])) { | |
$new_posts_columns = $posts_columns; | |
} else { | |
$new_posts_columns = array(); | |
$index = 0; | |
foreach($posts_columns as $key => $posts_column) { | |
if ($key=='author') { | |
$new_posts_columns['{column_name}'] = null; | |
} | |
$new_posts_columns[$key] = $posts_column; | |
} | |
} | |
$new_posts_columns['{column_name}'] = '{Column pretty name}'; | |
return $new_posts_columns; | |
} | |
// manage_pages_custom_column for hierarchical custom post type | |
// manage_posts_custom_column otherwise | |
add_action('manage_posts_custom_column', 'display_taxonomy_terms',10,2); | |
function display_taxonomy_terms($column_id,$post_id ) { | |
global $typenow; | |
if ($typenow=='{cutom_post_type_name}') { | |
$taxonomy = '{custom_taxonomy_name}'; | |
switch ($column_id) { | |
case '{column_name}': | |
$custom_terms = get_the_terms($post_id,$taxonomy); | |
if (is_array($custom_terms)) { | |
foreach($custom_terms as $key => $custom_term) { | |
$edit_link = get_term_link($custom_term,$taxonomy); | |
$custom_terms[$key] = '<a href="'.$edit_link.'">' . $custom_term->name . '</a>'; | |
} | |
echo implode(' | ', $custom_terms); | |
} | |
break; | |
} | |
} | |
} | |
/* JUST AN HYPOTHETICAL EXAMPLE | |
add_action('manage_posts_custom_column', 'manage_posts_custom_column',10,2); | |
function manage_posts_custom_column( $column_id,$post_id ) { | |
global $typenow; | |
switch ("{$typenow}:{$column_id}") { | |
case 'listing:business': | |
echo '...whatever...'; | |
break; | |
case 'listing:property': | |
echo '...whatever...'; | |
break; | |
case 'agent:listing': | |
echo '...whatever...'; | |
break; | |
} | |
} | |
*/ | |
add_action('restrict_manage_posts','restrict_custom_post_type_by_taxonomy'); | |
function restrict_custom_post_type_by_taxonomy() { | |
global $typenow; | |
global $wp_query; | |
if ($typenow=='{custom_post_type_name}') { | |
$taxonomy = '{custom_taxonomy_name}'; | |
$business_taxonomy = get_taxonomy($taxonomy); | |
wp_dropdown_categories(array( | |
'show_option_all' => __("Show All {$business_taxonomy->label}"), | |
'taxonomy' => $taxonomy, | |
'name' => '{custom_taxonomy_name}', | |
'orderby' => 'name', | |
'selected' => $wp_query->query['term'], | |
'hierarchical' => true, | |
'depth' => 3, | |
'show_count' => true, // This will give a view | |
'hide_empty' => true, // This will give false positives, i.e. one's not empty related to the other terms. TODO: Fix that | |
)); | |
} | |
} | |
add_filter('parse_query','convert_term_id_to_taxonomy_term_in_query'); | |
function convert_term_id_to_taxonomy_term_in_query($query) { | |
global $pagenow; | |
$qv = &$query->query_vars; | |
if ($pagenow=='edit.php' && | |
isset($qv['taxonomy']) && $qv['taxonomy']=='{custom_taxonomy_name}' && | |
isset($qv['term']) && is_numeric($qv['term'])) { | |
$term = get_term_by('id',$qv['term'],'{custom_taxonomy_name}'); | |
$qv['term'] = $term->slug; | |
} | |
} | |
//add_action('init','register_listing_post_type'); | |
//function register_listing_post_type() { | |
// register_post_type('listing',array( | |
// 'label' => 'Listings', | |
// 'public' => true, | |
// 'publicly_queryable' => true, | |
// 'show_ui' => true, | |
// 'query_var' => true, | |
// 'rewrite' => true, | |
// 'capability_type' => 'post', | |
// 'hierarchical' => false, | |
// )); | |
//} | |
//add_action('init','register_business_taxonomy'); | |
//function register_business_taxonomy() { | |
// register_taxonomy('business',array('listing'),array( | |
// 'label' => 'Businesses', | |
// 'public'=>true, | |
// 'hierarchical'=>true, | |
// 'show_ui'=>true, | |
// 'query_var'=>true | |
// )); | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment