Adding Taxonomy Columns To Admin Edit Page For Custom Post Types In WordPress

[deprecated slug=”adding-taxonomy-columns-to-admin-edit-pages-in-wordpress”]

I’ve been playing with custom post types and taxonomies for organizing all things not post and I wanted to customize my admin edit screens for each new custom post type. Navjot Singh’s tutorial[^1] and this thread on Theme Hybrid’s forums[^2] both provide an introduction to adding taxonomy columns. However, I am lazy and don’t like hardcoding in variables each time I add a custom post type or new taxonomy.

The following set of functions grabs a list of available taxonomies for a given `post_type` and automatically adds columns to the appropriate admin pages, without the need to hardcode each post type to taxonomy relationships.


if ( is_admin() && !empty( $_GET['post_type'] ) ) {
add_filter( 'manage_posts_columns' , 'ucc_manage_posts_columns' );
add_action( 'manage_posts_custom_column' , 'ucc_manage_posts_custom_column' , 10 , 2 );
}

function ucc_manage_posts_columns ( $defaults ) {
$post_type = get_query_var( 'post_type' );

$args = array(
'public' => true ,
'_builtin' => true
);
$output = 'names';
$operator = 'and';

$_builtin_post_types = get_post_types( $args , $output , $operator );

if ( !empty( $post_type ) && !in_array( $post_type , $_builtin_post_types ) ) {
$taxonomies = get_object_taxonomies( $post_type );
foreach ( $taxonomies as $taxonomy ) {
$t = get_taxonomy( $taxonomy );
if ( !in_array( $t->label , array( 'Post Tags' , 'Categories' ) ) )
$defaults[$taxonomy] = __( $t->label );
}
}

return $defaults;
}

function ucc_manage_posts_custom_column ( $column_name , $post_id ) {
$terms = get_the_terms( $post->ID , $column_name );

if ( !empty( $terms ) ) {
foreach ( $terms as $t )
$output[] = "slug'> " . esc_html( sanitize_term_field( 'name' , $t->name , $t->term_id , $column_name , 'display') ) . "";
$output = implode( ', ' , $output );
} else {
$t = get_taxonomy( $column_name );
$output = "No $t->label";
}

echo $output;
}

Updated 05 June 2010: Added management for `_builtin` post types.

[^1]: [Add custom taxonomy columns to edit posts page](http://nspeaks.com/1015/add-custom-taxonomy-columns-to-edit-posts-page/)
[^2]: [Add custom taxonomy to edit.php](http://themehybrid.com/support/topic/add-custom-taxonomy-to-editphp)

4 thoughts on “Adding Taxonomy Columns To Admin Edit Page For Custom Post Types In WordPress”

  1. Brilliant! Used this one immediately for a theme that uses custom taxonomies. The only drawback was that there was an artifact in the IDs column. There might be a conflict with an existing IDs plugin we’re using, but the end result is that there is a comma before the ID, when of course the ID is unique and therefore shouldn’t have a comma.

    The line that writes the comma is this:

    $output = implode( ', ' , $output );
    

    I’m not a PHP wiz enough to understand why it’s adding the comma. Perhaps if it is being passed only one member in the array, the comma appears; in which case it could be sanitized with a simple if/then in which implode is only called when the array has 2 or more members. Not sure! But I don’t mind space-separated, so in my deployment I’m just leaving it as ‘ ‘ instead of ‘, ‘.

  2. Not that you need to be a ‘wiz’ to understand implode() per se. 😉 Just posting back to let you know that this error keeps appearing on your page:

    Fatal error: Call to undefined function ucc_time_since() in /home/jmdodd/bajada.net/wp/wp-content/themes/ucc/library/extensions.php on line 332

  3. And for the third follow-up cause I’m spammy like that… it was owing to the output of a plugin. The plugin stores information for the ID column it generates in a multi-member array. Hence the comma. I would have to alter that plug-in or otherwise change how I generate an ID column to get rid of the comma. Not the code above’s fault at all!

Leave a Reply