vitaliikovalenko.com/blog

WordPress: How to add a custom culumn into the list of posts in the admin panel

For the general posts and pages we need to add a function into the theme's function.php file. And for the custom posts that function should be put into the custom post type's plugin file. The function itself and the filter that follows are quite simple:

function modify_cpt_table($column) {
    $column['field'] = 'Field name';
    return $column;
}
add_filter('manage_cpt_posts_columns', 'modify_cpt_table');

In this example cpt is the name of the custom post type. To work with the general posts it should be omitted. To work with pages “posts” in this example should be replaced with “pages”.


Even better code for the same thing

This code allows to put the new colum into the first position:

function modify_copy_table($columns) {
    $column_name = array('name'=>'Name');
    $columns = array_slice($columns, 0, 1, true) + $column_name + array_slice($columns, 1, NULL, true);
    return $columns;
}
add_filter('manage_copy_posts_columns', 'modify_copy_table');

It will put the new column on the second position right after the checkboxes.

Here is the code to fill the newly inserted column with data:

add_action('manage_copy_posts_custom_column', 'name_column_action', 10, 1);
function name_column_action($column) {
    global $post;
    switch($column) {
    case 'name':
        echo sanitize_text_field(get_post_meta($post->ID, 'copy_name', true));
        break;
    }
}

It will load the custom field data into the column.