WordPress Custom Columns

I have created a custom post type called Members.

I then wanted to customise the display for this custom post type to display some custom meta which has been entered in the columns.

Read More

The columns are displaying but the data isn’t showing up and I can’t work out why.

add_filter( 'manage_edit-members_columns', 'my_columns_filter', 10, 1 );

function my_columns_filter( $columns ) {
$column_phone = array( 'phone' => 'Phone Number' );
$column_email = array( 'email' => 'Email Address' );
$column_membertype = array( 'member_type' => 'Member Type' );
$columns = array_slice( $columns, 0, 2, true ) + $column_phone + array_slice( $columns, 2, NULL, true );
$columns = array_slice( $columns, 0, 3, true ) + $column_email + array_slice( $columns, 3, NULL, true );
$columns = array_slice( $columns, 0, 4, true ) + $column_membertype + array_slice( $columns, 4, NULL, true );
return $columns;
}

add_action( 'manage_members_custom_column', 'my_column_action', 10, 2 );

function my_column_action( $column, $post_id ) {
global $post;
switch ( $column ) {
    case 'phone':
        echo get_post_meta($post_id, '_cricketss_phone', TRUE);
        break;
    case 'email':
        echo get_post_meta($post_id, '_cricketss_email', TRUE);
        break;
    case 'member_type':
        echo get_post_meta($post_id, '_cricketss_phone', TRUE);
        break;
}
}

Any heads up would be appreciated.

Related posts

Leave a Reply

1 comment

  1. I’ll take a shot in the absence of the register_post_type code. Here are the things to try

    1. Add 'hierarchical' => false to your register_post_type if you haven’t already

    2. Change

      add_action( 'manage_members_custom_column', 'my_column_action', 10, 2 );
      

      to

      add_action( 'manage_posts_custom_column', 'my_column_action', 10, 2 );
      

    I have a strong feeling that your problem lies in the point 2 I mentioned.

    The Codex states:

    Combined with the manage_edit-${post_type}_columns filter, this allows you to add custom columns to the list post/page/custom post type pages. The filter described in here works both for built in post types as well as custom post types. Note that if the custom post type has ‘hierarchical’ => true, then the correct action hook to use is manage_pages_custom_column.