Custom Columns Don't Display Anything

1.4k views Asked by At

I have added a new column to a custom post type to display the post's ID. This works for the WordPress core post type, but not for my custom post type. I have tried using the manage_{post_type}_custom_column hook and just applying it to all posts, but neither works.

It DOES add the custom column headers, but I can't populate them with anything at all when viewing the custom post type.

This is what it looks like when viewing the custom post type and this is what it looks like when viewing a regular core post.

//  Add post ID column to use an order ID in all posts view.

add_filter('manage_posts_columns', 'oms_order_id_header');
add_action('manage_posts_custom_column', 'oms_order_id_column', 10, 2);

function oms_order_id_header($columns) {
    //Remove title column
    //unset($columns['title']);
    //Add new columns
    $columns['order_id_header'] = 'Order ID';
    $columns['customer_header'] = 'Customer';
    $columns['author']          = 'Owner';    

    return $columns;
}

function oms_order_id_column( $column, $post_id ) {
        if ($column == 'order_id_header') {
            echo $post_id;
        }
}
2

There are 2 answers

0
Alejandro Sanchez On

I just tried you code and it seems to be working perfect on my WordPress installation in both cases: Custom Post and Post.

Maybe your error is happening because your posts are drafts? I don't think so but maybe. (I tried your code also with drafts on my installation and it worked). Here is the screenshot: screenshot

Try printing "hello world" instead of the $post_id to check if it prints in all cases.

0
user1607523 On

This turned out to be an issue with the post type being set to hierarchical. Hierarchical post types need to be targeted by a different action hook than the one used here.

Instead of manage_posts_custom_column, hierarchical post types need to use manage_pages_custom_column.