Many WordPress settings, shortcodes and conditions ask for a post or page ID. WordPress hides it: you hover over a title or open the post and read the ID from the URL.
This snippet shows the post, page and custom post type ID in its own column in the WordPress admin lists, right after the title.
The snippet
<?php
/**
* ID column in the wp-admin lists of posts, pages and custom post types.
*/
add_filter( 'manage_posts_columns', 'dpc_post_id_column_add' );
add_filter( 'manage_pages_columns', 'dpc_post_id_column_add' );
add_action( 'manage_posts_custom_column', 'dpc_post_id_column_render', 10, 2 );
add_action( 'manage_pages_custom_column', 'dpc_post_id_column_render', 10, 2 );
add_action( 'admin_head-edit.php', 'dpc_post_id_column_width' );
function dpc_post_id_column_add( $columns ) {
$new = array();
foreach ( $columns as $key => $label ) {
$new[ $key ] = $label;
if ( 'title' === $key ) {
$new['dpc_post_id'] = esc_html__( 'ID', 'dpc' );
}
}
if ( ! isset( $new['dpc_post_id'] ) ) {
$new['dpc_post_id'] = esc_html__( 'ID', 'dpc' );
}
return $new;
}
function dpc_post_id_column_render( $column, $post_id ) {
if ( 'dpc_post_id' === $column ) {
echo absint( $post_id );
}
}
function dpc_post_id_column_width() {
echo '<style>.fixed .column-dpc_post_id{width:5em}</style>';
}
How it works
- WordPress builds the list columns with two filters:
manage_pages_columnsfor Pages andmanage_posts_columnsfor Posts and every custom post type. - The cells use two other hooks:
manage_pages_custom_columnfor hierarchical post types (Pages, and custom post types with parent items) andmanage_posts_custom_columnfor the rest. Hooking all four covers every list. - The column is added right after Title, so it is visible without scrolling. It falls back to the end of the list if a post type has no title column.
- The ID is printed with
absint(), and the column gets a fixed width of 5em so it does not take space from the title. - Like any column, it can be switched off per user in Screen Options.
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Admin only, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty. The list tables only exist in wp-admin, so Admin only is all it needs.
The same code also works unchanged in a small plugin or an mu-plugin.
Customise it
- Sort the lists by ID: Add a Sortable ID Column to WordPress Admin Lists is the sortable version. Use one of the two, not both, or you get two ID columns.
- IDs in the Media list view too: add
manage_media_columnsandmanage_media_custom_columnwith the same two functions. - Category and tag IDs: Add Taxonomy Columns to WordPress Admin Post Lists shows the taxonomy side of admin columns.
Gotchas
- Plugins that build their own list screens (WooCommerce orders with HPOS, form entries) do not use these hooks and show no ID column.
- The old version used the key
wps_post_id. If you hid that column in Screen Options, the newdpc_post_idcolumn shows up again once.
In the Scripts Organizer library Import it with one click from Scripts Organizer.