Display Custom Column in CPT Taxonomy

admin2025-01-07  4

I am trying to add a column to one of my custom post types, but only on the edit taxonomy page.

I have a taxonomy registered called "event-categories" for my custom post type "events".

I can successfully add the custom column to the custom post type edit screen, but can't seem to get the hook to work on the edit taxonomy page.

Digging through some of the core, I was able to locate the hook

$taxonomies = apply_filters( "manage_taxonomies_for_{$post_type}_columns", $taxonomies, $post_type );

inside of class-wp-posts-list-table.php which would lead me to believe it is possible to hook into, and add custom columns to this page. But when hooking in to add the column, nothing actually happenes.

function events_color_column($defaults) {
    $defaults['event_cat_color'] = 'Event Category Color';
    return $defaults;
}

function events_column_content($column_name, $post_ID) {
    if ($column_name == 'event_cat_color') {
       echo 'Event Color : #2432';
    }
}
add_filter('manage_taxonomies_for_events_columns', 'events_color_column');
add_action('manage_taxonomies_for_events_column', 'events_color_column_content', 10, 2);

but if I simply change the filter+action to 'manage_events_columns' and 'manage_events_column' things work on the custom post type screen.

Can someone see what I am doing wrong here, or is this a bug that needs to be patched in core?

Edit:

I was able to hook into the custom taxonomy page to display the column header, by doing the following:

To any one else facing similar issues, you need to pass in the page ID. For me, the following worked:

  add_filter('manage_edit-event_categories_columns', 'events_color_column');

But I can't seem to figure out why the content isn't printing into each column using

  add_action('manage_edit-event_categories_custom_column', 'events_color_column_content', 10, 2);

I have also tried passing in the taxonomy name, the post type name, but nothing seems to work. What am I missing here?

I am trying to add a column to one of my custom post types, but only on the edit taxonomy page.

I have a taxonomy registered called "event-categories" for my custom post type "events".

I can successfully add the custom column to the custom post type edit screen, but can't seem to get the hook to work on the edit taxonomy page.

Digging through some of the core, I was able to locate the hook

$taxonomies = apply_filters( "manage_taxonomies_for_{$post_type}_columns", $taxonomies, $post_type );

inside of class-wp-posts-list-table.php which would lead me to believe it is possible to hook into, and add custom columns to this page. But when hooking in to add the column, nothing actually happenes.

function events_color_column($defaults) {
    $defaults['event_cat_color'] = 'Event Category Color';
    return $defaults;
}

function events_column_content($column_name, $post_ID) {
    if ($column_name == 'event_cat_color') {
       echo 'Event Color : #2432';
    }
}
add_filter('manage_taxonomies_for_events_columns', 'events_color_column');
add_action('manage_taxonomies_for_events_column', 'events_color_column_content', 10, 2);

but if I simply change the filter+action to 'manage_events_columns' and 'manage_events_column' things work on the custom post type screen.

Can someone see what I am doing wrong here, or is this a bug that needs to be patched in core?

Edit:

I was able to hook into the custom taxonomy page to display the column header, by doing the following:

To any one else facing similar issues, you need to pass in the page ID. For me, the following worked:

  add_filter('manage_edit-event_categories_columns', 'events_color_column');

But I can't seem to figure out why the content isn't printing into each column using

  add_action('manage_edit-event_categories_custom_column', 'events_color_column_content', 10, 2);

I have also tried passing in the taxonomy name, the post type name, but nothing seems to work. What am I missing here?

Share Improve this question edited Oct 10, 2014 at 16:25 EHerman asked Oct 10, 2014 at 15:42 EHermanEHerman 9992 gold badges16 silver badges33 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

The final working hooks that I was able to work out are:

// adding an extra column to the event_cateogires page
// to display the color
function events_color_column($defaults) {
    $defaults['event_cat_color'] = 'Event Category Color';
    return $defaults;
}

function events_color_column_content($column_name, $post_ID) {  
    echo 'Event Color : #2432';
}
add_filter('manage_edit-event_categories_columns', 'events_color_column');
add_action('manage_event_categories_custom_column', 'events_color_column_content', 10, 2);

Using the

if ($column_name == 'event_cat_color') {
       echo 'Event Color : #2432';
    }

didn't seem to work with this action hook. Removing the conditional, and just echoing things out seems to work.

minor issues in your code that need to be corrected

In the add_action call, you are using the wrong function name

events_color_column_content instead of events_column_content

The hook you are using should be manage_edit-{taxonomy}_custom_column for custom taxonomy columns, not manage_edit-{taxonomy}_columns.

function events_color_column($columns) {
    $columns['event_cat_color'] = 'Event Category Color';
    return $columns;
}

function events_column_content($content, $column_name, $term_id) {
    if ($column_name == 'event_cat_color') {
       $content = 'Event Color : #2432';
    }
    return $content;
}

add_filter('manage_edit-event_categories_columns', 'events_color_column');
add_action('manage_event_categories_custom_column', 'events_column_content', 10, 3);

To display a custom column in the admin panel for a Custom Post Type (CPT) taxonomy, you can use the following steps:

1. Add a New Column

Hook into the manage_edit-{$taxonomy}_columns filter to add a new column. Replace {$taxonomy} with your taxonomy slug.

add_filter('manage_edit-your_taxonomy_columns', function ($columns) {
    $columns['custom_column'] = __('Custom Column'); // Add your column
    return $columns;
});

2. Populate the Column with Data

Hook into the manage_{$taxonomy}_custom_column action to display data in the new column.

add_action('manage_your_taxonomy_custom_column', function ($content, $column_name, $term_id) {
    if ('custom_column' === $column_name) {
        $custom_data = get_term_meta($term_id, 'custom_meta_key', true); // Replace with your meta key
        $content = $custom_data ? esc_html($custom_data) : __('No data');
    }
    return $content;
}, 10, 3);

3. Optional: Make the Column Sortable

To make the column sortable, use the manage_edit-{$taxonomy}_sortable_columns filter.

add_filter('manage_edit-your_taxonomy_sortable_columns', function ($sortable) {
    $sortable['custom_column'] = 'custom_meta_key'; // Replace with your meta key
    return $sortable;
});

Replace your_taxonomy with your taxonomy slug and custom_meta_key with the meta key used to store the data.

Example Usage

If you have a taxonomy product_category and want to show a custom field extra_info in the admin columns:

// Add the column
add_filter('manage_edit-product_category_columns', function ($columns) {
    $columns['extra_info'] = __('Extra Info');
    return $columns;
});

// Populate the column
add_action('manage_product_category_custom_column', function ($content, $column_name, $term_id) {
    if ('extra_info' === $column_name) {
        $extra_info = get_term_meta($term_id, 'extra_info', true);
        $content = $extra_info ? esc_html($extra_info) : __('No data');
    }
    return $content;
}, 10, 3);

This will display a column with your custom meta data in the taxonomy admin screen.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253488a126.html

最新回复(0)