Created
August 21, 2010 00:21
-
-
Save mikeschinkel/541505 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_listing_posts_columns', 'add_businesses_column_to_listing_list'); | |
function add_businesses_column_to_listing_list( $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['businesses'] = null; | |
} | |
$new_posts_columns[$key] = $posts_column; | |
} | |
} | |
$new_posts_columns['businesses'] = 'Businesses'; | |
return $new_posts_columns; | |
} | |
add_action('manage_posts_custom_column', 'show_businesses_column_for_listing_list',10,2); | |
function show_businesses_column_for_listing_list( $column_id,$post_id ) { | |
global $typenow; | |
if ($typenow=='listing') { | |
$taxonomy = 'business'; | |
switch ($column_id) { | |
case 'businesses': | |
$businesses = get_the_terms($post_id,$taxonomy); | |
if (is_array($businesses)) { | |
foreach($businesses as $key => $business) { | |
$edit_link = get_term_link($business,$taxonomy); | |
$businesses[$key] = '<a href="'.$edit_link.'">' . $business->name . '</a>'; | |
} | |
echo implode(' | ',$businesses); | |
} | |
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_listings_by_business'); | |
function restrict_listings_by_business() { | |
global $typenow; | |
global $wp_query; | |
if ($typenow=='listing') { | |
$taxonomy = 'business'; | |
$business_taxonomy = get_taxonomy($taxonomy); | |
wp_dropdown_categories(array( | |
'show_option_all' => __("Show All {$business_taxonomy->label}"), | |
'taxonomy' => $taxonomy, | |
'name' => 'business', | |
'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_business_id_to_taxonomy_term_in_query'); | |
function convert_business_id_to_taxonomy_term_in_query($query) { | |
global $pagenow; | |
$qv = &$query->query_vars; | |
if ($pagenow=='edit.php' && | |
isset($qv['taxonomy']) && $qv['taxonomy']=='business' && | |
isset($qv['term']) && is_numeric($qv['term'])) { | |
$term = get_term_by('id',$qv['term'],'business'); | |
$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 | |
)); | |
} |
Can anybody please let me know how to use this code? cause I am new be in wordpress. and want to do add filter to custom category.
Is this still ok to use this code for current 4.8 Wordpress version?
Thanks chodorowich for your code example in the comments. I noticed that it only filters the first taxonomy if 2 are selected do you have suggestions on how I could fix this?
You should make this into a plugin.
it's much easier now in WP 3.1
function my_restrict_manage_posts() { global $typenow; $args=array( 'public' => true, '_builtin' => false ); $post_types = get_post_types($args); if ( in_array($typenow, $post_types) ) { $filters = get_object_taxonomies($typenow); foreach ($filters as $tax_slug) { $tax_obj = get_taxonomy($tax_slug); wp_dropdown_categories(array( 'show_option_all' => __('Show All '.$tax_obj->label ), 'taxonomy' => $tax_slug, 'name' => $tax_obj->name, 'orderby' => 'term_order', 'selected' => $_GET[$tax_obj->query_var], 'hierarchical' => $tax_obj->hierarchical, 'show_count' => false, 'hide_empty' => true )); } } } function my_convert_restrict($query) { global $pagenow; global $typenow; if ($pagenow=='edit.php') { $filters = get_object_taxonomies($typenow); foreach ($filters as $tax_slug) { $var = &$query->query_vars[$tax_slug]; if ( isset($var) ) { $term = get_term_by('id',$var,$tax_slug); $var = $term->slug; } } } } add_action('restrict_manage_posts', 'my_restrict_manage_posts' ); add_filter('parse_query','my_convert_restrict');
This code worked perfectly for me in 2020 using WP 5.4! My situation is as follows:
I've created a custom post type Jobs and added two taxonomies (Branche and Status). I needed a filter to show in the admin to quickly filter the status (open/closed) within a branche. Happy to got it working!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
YES!
:D