Adding custom columns to custom post types

I've done this previously but I've forgotten the name of the hook, and can't find it anywhere...

What I'm trying to do is add some custom columns in the listing of a custom post type in the admin.

For example, in the admin, click on articles, I want to add custom column there.

Topic columns wp-admin Wordpress

Category Web


This guide worked for me. In my case, I'm using CPT UI to create the custom post types, ACF to create the custom post fields, and Code Snippets to glue everything together.

I've included a simplified version of the code below.

Change custom-post-type-slug, custom_post_type_slug, and custom_column_name below to suit your needs. Be sure to maintain the same dash/underscore format when changing the names.

Note that get_field() is specific to the ACF plugin. If you're not using ACF, you may want to use the built-in get_post_meta() WordPress function to retrieve and display post metadata.

add_filter('manage_custom-post-type-slug_posts_columns', 'set_custom_edit_custom_post_type_slug_columns');

function set_custom_edit_custom_post_type_slug_columns($columns) {
    $columns['custom_column_name'] = 'Custom Column Title';
    return $columns;
}

add_action('manage_custom-post-type-slug_posts_custom_column' , 'custom_custom_post_type_slug_column', 10, 2);

function custom_custom_post_type_slug_column($column, $post_id) {
    switch ($column) {
        case 'custom_column_name':
            echo get_field('custom_column_name', $post_id);
            break;
    }
}

add_filter('manage_edit-custom-post-type-slug_sortable_columns', 'set_custom_custom_post_type_slug_sortable_columns');

function set_custom_custom_post_type_slug_sortable_columns($columns) {
    $columns['custom_column_name'] = 'custom_column_name';
    return $columns;
}

add_action('pre_get_posts', 'custom_post_type_slug_custom_orderby');

function custom_post_type_slug_custom_orderby($query) {
    if ( ! is_admin()) {
        return;
    }

    $orderby = $query->get('orderby');

    if ('custom_column_name' == $orderby) {
        $query->set('meta_key', 'custom_column_name');
        $query->set('orderby', 'meta_value_num');
    }
}

For woocommerce orders I needed to use the filter: manage_edit-shop_order_columns.

add_filter( 'manage_edit-shop_order_columns', 'manage_shop_order_posts_columns', 10, 1 );
manage_shop_order_posts_columns($columns) {
    $columns['test'] = 'Test';
    return $columns;
}

Then to populate the field:

add_action( 'manage_shop_order_posts_custom_column' , 'manage_shop_order_custom_column', 10, 2 );
function manage_shop_order_custom_column($column, $post_id) {
    switch ( $column )
    {
        case 'test' :
            echo '1234';
            break;
    }
}

Based on Carlos response (thank you very much) I wanted to add a little thumbnail image, but not a featured one, one from custom metabox.

So this is what I added:

add_admin_column(__('Thumbnail'), 'obraz', function($post_id){
$image_id =  get_post_meta( $post_id , 'custom_thumbnail_metabox' , true );
echo '<img src="'.wp_get_attachment_image_url($image_id).'" />';

Info: the post meta will get the ID of the attachment/image so thats the reason for later call of wp_get_attachment_image_url


I wrote a function that combines manage_{$post_type}_posts_columns filter and manage_{$post_type}_posts_custom_column action.

function add_admin_column($column_title, $post_type, $cb){

    // Column Header
    add_filter( 'manage_' . $post_type . '_posts_columns', function($columns) use ($column_title) {
        $columns[ sanitize_title($column_title) ] = $column_title;
        return $columns;
    } );

    // Column Content
    add_action( 'manage_' . $post_type . '_posts_custom_column' , function( $column, $post_id ) use ($column_title, $cb) {

        if(sanitize_title($column_title) === $column){
            $cb($post_id);
        }

    }, 10, 2 );
}

Usage:

add_admin_column(__('EAN'), 'product', function($post_id){
    echo get_post_meta( $post_id , 'ean' , true ); 
});

I'm not sure if it default custom meta data that you want to show as columns, but you could consider to use this free plugin that allows you to add columns to display custom fields. https://wordpress.org/plugins/codepress-admin-columns/

The pro version even allows you to add filtering, sorting and inline edit to those columns.


The hooks to create custom columns and their associated data for a custom post type are manage_{$post_type}_posts_columns and manage_{$post_type}_posts_custom_column respectively, where {$post_type} is the name of the custom post type.

This example from the documentation removes the author column and adds a taxonomy and meta data column:

// Add the custom columns to the book post type:
add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' );
function set_custom_edit_book_columns($columns) {
    unset( $columns['author'] );
    $columns['book_author'] = __( 'Author', 'your_text_domain' );
    $columns['publisher'] = __( 'Publisher', 'your_text_domain' );

    return $columns;
}

// Add the data to the custom columns for the book post type:
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function custom_book_column( $column, $post_id ) {
    switch ( $column ) {

        case 'book_author' :
            $terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' );
            if ( is_string( $terms ) )
                echo $terms;
            else
                _e( 'Unable to get author(s)', 'your_text_domain' );
            break;

        case 'publisher' :
            echo get_post_meta( $post_id , 'publisher' , true ); 
            break;

    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.