I found this on wordpress.org forums. tested it and works fine. When i changed the variables to my own it failed. I checked it thoroughly (!) for hours. i can’t see where it’s going wrong.
register_post_type( 'book', array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
'add_new' => 'Add New',
'add_new_item' => 'Add New Book',
'edit_item' => 'Edit book',
'edit' => 'Edit',
'new_item' => 'New Book',
'view_item' => 'View Book',
'search_items' => 'Search Books',
'not_found' => 'No books found',
'not_found_in_trash' => 'No books found in Trash',
'view' => 'View Book'
),
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'query_var' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'comments' ),
'taxonomies' => array('category','post_tag')
)
);
register_taxonomy(
'book_category',
'book',
array(
'hierarchical' => true,
'label' => 'Categoriën',
'public' => true,
'query_var' => true,
'show_tagcloud' => true,
'rewrite' => Array(
'slug' => 'book_category')
)
);
function add_new_columns($defaults) {
$defaults['book_cats'] = __('Categoriën');
return $defaults;
}
function add_column_data( $column_name, $post_id ) {
if( $column_name == 'book_cats' ) {
$_posttype = 'book';
$_taxonomy = 'book_category';
$terms = get_the_terms( $post_id, $_taxonomy );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $c )
$_taxonomy_title = esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display'));
$out[] = "<a href='edit.php?book_category=$_taxonomy_title&post_type=$_posttype'>$_taxonomy_title</a>";
echo join( ', ', $out );
}
else {
_e('Uncategorized');
}
}
}
add_filter( 'manage_book_posts_columns', 'add_new_columns' );
add_action( 'manage_posts_custom_column', 'add_column_data', 10, 2 ); ?>
My edited version (the newly “adjusted” one that now does something!):
register_post_type( 'frog', array(
'labels' => array(
'name' => 'Frog',
'singular_name' => 'Frog',
'add_new' => 'Add New',
'add_new_item' => 'Add New frog',
'edit_item' => 'Edit frog',
'edit' => 'Edit',
'new_item' => 'New frog',
'view_item' => 'View frog',
'search_items' => 'Search frogs',
'not_found' => 'No frogs found',
'not_found_in_trash' => 'No frogs found in Trash',
'view' => 'View frog'
),
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'query_var' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'comments' ),
'taxonomies' => array('category','post_tag')
)
);
register_taxonomy(
'frog_category',
'frog',
array(
'hierarchical' => true,
'label' => 'Categories',
'public' => true,
'query_var' => true,
'show_tagcloud' => true,
'rewrite' => Array(
'slug' => 'frog_category')
)
);
function add_new_frogcolumns($defaults) {
$defaults['frog_cats'] = __('Categories');
return $defaults; //missing as pointed out by scribu
}
function add_frogcolumn_data( $column_name, $post_id ) {
if( $column_name == 'frog_cats' ) {
$_posttype = 'frog';
$_taxonomy = 'frog_category';
$terms = get_the_terms( $post_id, $_taxonomy );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $c )
$_taxonomy_title = esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display'));
$out[] = "<a href='edit.php?frog_category=$_taxonomy_title&post_type=$_posttype'>$_taxonomy_title</a>";
echo join( ', ', $out );
}
else {
_e('Uncategorized');
}
}
}
add_filter( 'manage_frog_posts_columns', 'add_new_frogcolumns' );
add_action( 'manage_posts_custom_column', 'add_frogcolumn_data', 10, 2 );//this line was also wrong
That script doesn’t allow sorting, just displaying a custom column. Is that the original script or the modified one?
Note that register_post_type() and register_taxonomy() shouldn’t be called directly, but in a callback hooked to the ‘init’ action. Working version:
i know this is old, but i stumbled here via google. i was looking for something else, but in case anyone needs it:
http://shibashake.com/wordpress-theme/add-custom-post-type-columns
is some really great info on how to add custom columns for CPTs. strange looking website but some totally badass WP info.
and this is one of justin tadlock’s tuts.. and it goes a step further to making things sortable. Custom columns for custom post types
i’m finding that only some columns are sortable.. i think b/c it is a modified query. i have yet to figure out how to sort by taxonomy like i’d like.