My custom post name is “employee”.
Here is the code for creating and populating admin columns.
Columns have been created but it is not populating. Also, what is the meaning of the “10, 2” in action hook? N.B. location and age values come from custom meta boxes.
add_filter( 'manage_employee_posts_columns', 'set_custom_edit_employee_columns' );
add_action( 'manage_employee_posts_custom_column' , 'custom_employee_column', 10, 2 );
function set_custom_edit_employee_columns($columns) {
unset( $columns['location'] );
$columns['location'] = __( 'Location', 'tm_cp' );
$columns['age'] = __( 'Age', 'tm_cp' );
return $columns;
}
function custom_employee_column( $column, $post_id ) {
switch ( $column ) {
case 'location' :
echo get_post_meta( $post_id , 'location' , true );
break;
case 'age' :
echo get_post_meta( $post_id , 'age' , true );
break;
}
}
1 Answer
The 10 is the priority, 2 means that two variables are passed on to the function ( $column, $post_id ). I think the problem is trying to echo get_post_meta
directly.
Try this:
function custom_employee_column( $column, $post_id ) {
switch ( $column ) {
case 'location' :
$metaData = get_post_meta( $post_id , 'location' , true );
echo $metaData;
break;
case 'age' :
$metaData = get_post_meta( $post_id , 'age' , true );
echo $metaData;
break;
}
}