Show Post, Page and CPT IDs in WordPress Admin Columns

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_columns for Pages and manage_posts_columns for Posts and every custom post type.
  • The cells use two other hooks: manage_pages_custom_column for hierarchical post types (Pages, and custom post types with parent items) and manage_posts_custom_column for 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

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 new dpc_post_id column shows up again once.

In the Scripts Organizer library Import it with one click from Scripts Organizer.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.