4

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 1

1

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;
    }
}
2
  • Thanks for your help. But it's not working. no data is showing in the columns. I think the function does not get the post id. How could I pass the post Id to the function?
    – rushdi
    Feb 17, 2016 at 6:32
  • OK. silly mistake. :) Now it's working. Thank You.
    – rushdi
    Feb 17, 2016 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.