Add a Sortable ID Column to WordPress Admin Lists

WordPress only shows a post’s ID when you hover the title and read the edit link in the status bar, which is slow when you need IDs for shortcodes, queries or support tickets. This snippet adds a sortable ID column to every WordPress admin list: posts, pages and custom post types.

The snippet

<?php
/**
 * Sortable ID column on every post type list in wp-admin.
 */
add_action( 'admin_init', 'dpc_register_id_columns' );

function dpc_register_id_columns(): void {
	$skip = array( 'scorg', 'scorg_scss', 'scorg_ga' );

	foreach ( get_post_types( array( 'show_ui' => true ) ) as $post_type ) {
		if ( in_array( $post_type, $skip, true ) ) {
			continue;
		}

		add_filter( "manage_{$post_type}_posts_columns", 'dpc_add_id_column' );
		add_action( "manage_{$post_type}_posts_custom_column", 'dpc_render_id_column', 10, 2 );
		add_filter( "manage_edit-{$post_type}_sortable_columns", 'dpc_sortable_id_column' );
	}
}

function dpc_add_id_column( array $columns ): array {
	$columns['dpc_id'] = esc_html__( 'ID', 'dpc' );
	return $columns;
}

function dpc_render_id_column( string $column, int $post_id ): void {
	if ( 'dpc_id' === $column ) {
		echo absint( $post_id );
	}
}

function dpc_sortable_id_column( array $columns ): array {
	$columns['dpc_id'] = 'ID';
	return $columns;
}

add_action( 'admin_head-edit.php', 'dpc_id_column_width' );

function dpc_id_column_width(): void {
	echo '<style>.fixed .column-dpc_id{width:5em}</style>';
}

How it works

  • On admin_init, when all post types are registered, the snippet loops over every post type that has an admin screen (show_ui) and hooks three things for each: the column header, the cell output and the list of sortable columns.
  • Scripts Organizer’s own post types (scorg, scorg_scss, scorg_ga) are skipped, since their lists have their own layout.
  • Clicking the column header adds orderby=ID to the URL, which WP_Query sorts on natively, so no extra query code is needed.
  • The cell prints absint( $post_id ), and the header text is escaped and translatable.
  • A one-line style on list screens keeps the column narrow.

The older One-Click Import version declared its functions inside a closure, which fatals with “Cannot redeclare” if that closure ever runs twice, used unprefixed names and was not sortable. This version declares top-level dpc_ functions and only hooks post types that have an admin list.

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. Admin only code is loaded on wp-admin requests only, which is exactly where the column lives, and the hooks run later on admin_init, when post types are registered.

The same code also works unchanged in a small plugin or an mu-plugin.

Customise it

  • ID first, right after the checkbox: in dpc_add_id_column() build a new array with cb, then dpc_id, then the remaining columns.
  • Only some post types: replace get_post_types( array( 'show_ui' => true ) ) with a list such as array( 'post', 'page', 'product' ).
  • Media Library list view: it uses manage_media_columns and manage_media_custom_column; hook dpc_add_id_column and dpc_render_id_column to those as well.

This is the sortable successor of Page, Post, and CPT ID in Admin columns. For more list columns see Show featured image in admin posts and pages table and Add Taxonomy to Admin columns.

Gotchas

  • Each user can hide the column under Screen Options on any list screen.
  • WooCommerce orders with HPOS use their own list screen, not a post type list, so they don’t get the column; the order number there is already the ID.
  • A post type registered after admin_init (rare) won’t get the column.

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.