Code snippet to display ID gives critical error

The question:

Based on this post, I added the code snippet below to my WordPress site, as to display the ID in the Posts section of each post. However, it returns “critical error”. Any idea what is wrong with this code?

add_filter( 'manage_posts_columns', 'column_register_wpse_101322' );
add_filter( 'manage_posts_custom_column', 'column_display_wpse_101322', 10, 3 );

function column_register_wpse_101322( $columns ) {
    $columns['uid'] = 'ID';
    return $columns;
}

function column_display_wpse_101322( $empty, $column_name, $post_id ) {
    if ('uid' != $column_name) {return $empty;}
    return "$post_id";
}

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Unfortunately you can’t just copy code that plays a completely different role and replace users with posts and somehow expect this to do what you want it. The reason you get a fatal error is because you’re passing in too many arguments (3) to the filter, when there are only 2 supplied.

Here’s the code to achieve what you need, if I understand you correctly:


add_filter( 'manage_posts_columns', 'column_register_wpse_101322' );
add_action( 'manage_posts_custom_column', 'column_display_wpse_101322', 10, 2 );

function column_register_wpse_101322( $columns ) {
    $columns[ 'uid' ] = 'ID';
    return $columns;
}

function column_display_wpse_101322( $column_name, $post_id ) {
    if ( 'uid' === $column_name ) {
        echo $post_id;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment