2024 Update
When registering custom taxonomies with the register_taxonomy()
function and you want a custom admin column for it, you can follow different approaches based on what you are trying to achieve:
1. Automated column method
The easiest way, as others described is to set the show_admin_column
parameter to true
while registering the taxonomy. This will make WordPress create the admin column and render all the terms automatically for each row.
add_action( 'init', 'so8090996_register_taxonomy' );
function so8090996_register_taxonomy() {
/* or 'post' / 'page' */
register_taxonomy( 'your_custom_taxonomy', array( 'your_post_type' ), array(
'labels' => array( /* ... */ ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true, // create column automatically
/* ... */
);
}
a) If you don't want to customize the rendered terms
You are finished. Everything is rendered automatically.
b) If you want to customize the rendered terms in the rows
Columns created this automated way are NOT seen as custom columns by WordPress, so you can NOT render values (terms) for the rows with ANY of the manage_[...]_custom_column
actions.
However, you can use the post_column_taxonomy_links
filter to override the rendering of term links in this column:
add_filter( 'post_column_taxonomy_links', 'so8090996_custom_taxonomy_links', 10, 3 );
function so8090996_custom_taxonomy_links( $term_links, $taxonomy, $terms ) {
/* This function filters all the taxonomies in all post lists, so only edit yours */
if ($taxonomy === 'your_custom_taxonomy' && is_array( $terms ) ) {
$term_links = array();
foreach ( $terms as $t ) {
$term_name = 'Custom ' . $t->name; // modify term name as you want
$term_link_params = array(
'post_type' => 'your_post_type', /* CHANGE to 'post'/'page' or any CPT */
$t->taxonomy => $t->slug,
);
$term_link = add_query_arg( $term_link_params, 'edit.php' );
$term_links[] = sprintf( '<a href="%s">%s</a>', $term_link, $term_name );
}
return $term_links;
}
/* Return all others */
return $term_links;
}
2. Manual column creation and data rendering
If you want even more customization, and you want full control over the rendering of terms, you can create the column manually and use the manage_[...]_custom_column
actions to render the column contents in each row.
First, make sure the admin column is not rendered automatically, by setting the show_admin_column
to false
in register_taxonomy()
.
Create the column with the manage_posts_columns
(for all lists) or manage_{$post_type}_posts_columns
(for specific post type) filter.
add_filter( 'manage_{your_post_type}_posts_columns', 'so8090996_create_custom_column' );
function so8090996_create_custom_column( $columns ) {
$columns['your_custom_taxonomy'] = 'Column Title';
return $columns;
}
WARNING: If you use 'taxonomy-your_custom_taxonomy' key for adding the column above, WordPress will think it is a taxonomy column automatically created by itself, an you will - again - not be able to render the column values with manage_[...]_custom_column
actions, only edit them with the post_column_taxonomy_links
filter, just like in 1./b) method.
Render the column values with the manage_posts_custom_column
(for all lists) or manage_{$post->post_type}_posts_custom_column
(for specific post type) action.
add_action( 'manage_{your_post_type}_posts_custom_column', 'so8090996_manage_custom_column', 10, 2 );
function so8090996_manage_custom_column( $column_name, $post_id ) {
if ( $column_name === 'your_custom_taxonomy' ) {
$terms = get_terms( array (
'taxonomy' => 'your_custom_taxonomy',
'object_ids' => $post_id,
/* any WP_Term_Query params */
) );
$term_links = array();
foreach ( $terms as $t ) {
$term_name = 'Custom ' . $t->name; // modify term name as you want
$term_link_params = array(
'post_type' => 'your_post_type', /* CHANGE to 'post'/'page' or any CPT */
$t->taxonomy => $t->slug,
);
$term_link = add_query_arg( $term_link_params, 'edit.php' );
$term_links[] = sprintf( '<a href="%s">%s</a>', $term_link, $term_name );
}
echo implode( ', ', $term_links );
}
}