{"id":46631,"date":"2026-10-10T17:17:36","date_gmt":"2026-10-10T17:17:36","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46631"},"modified":"2026-10-10T17:17:36","modified_gmt":"2026-10-10T17:17:36","slug":"wordpress-admin-sortable-id-column","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-admin-sortable-id-column\/","title":{"rendered":"Add a Sortable ID Column to WordPress Admin Lists"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WordPress only shows a post&#8217;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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Sortable ID column on every post type list in wp-admin.\n *\/\nadd_action( 'admin_init', 'dpc_register_id_columns' );\n\nfunction dpc_register_id_columns(): void {\n\t$skip = array( 'scorg', 'scorg_scss', 'scorg_ga' );\n\n\tforeach ( get_post_types( array( 'show_ui' =&gt; true ) ) as $post_type ) {\n\t\tif ( in_array( $post_type, $skip, true ) ) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tadd_filter( \"manage_{$post_type}_posts_columns\", 'dpc_add_id_column' );\n\t\tadd_action( \"manage_{$post_type}_posts_custom_column\", 'dpc_render_id_column', 10, 2 );\n\t\tadd_filter( \"manage_edit-{$post_type}_sortable_columns\", 'dpc_sortable_id_column' );\n\t}\n}\n\nfunction dpc_add_id_column( array $columns ): array {\n\t$columns&#91;'dpc_id'&#93; = esc_html__( 'ID', 'dpc' );\n\treturn $columns;\n}\n\nfunction dpc_render_id_column( string $column, int $post_id ): void {\n\tif ( 'dpc_id' === $column ) {\n\t\techo absint( $post_id );\n\t}\n}\n\nfunction dpc_sortable_id_column( array $columns ): array {\n\t$columns&#91;'dpc_id'&#93; = 'ID';\n\treturn $columns;\n}\n\nadd_action( 'admin_head-edit.php', 'dpc_id_column_width' );\n\nfunction dpc_id_column_width(): void {\n\techo '&lt;style&gt;.fixed .column-dpc_id{width:5em}&lt;\/style&gt;';\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>On <code>admin_init<\/code>, when all post types are registered, the snippet loops over every post type that has an admin screen (<code>show_ui<\/code>) and hooks three things for each: the column header, the cell output and the list of sortable columns.<\/li>\n\n\n<li>Scripts Organizer&#8217;s own post types (<code>scorg<\/code>, <code>scorg_scss<\/code>, <code>scorg_ga<\/code>) are skipped, since their lists have their own layout.<\/li>\n\n\n<li>Clicking the column header adds <code>orderby=ID<\/code> to the URL, which <code>WP_Query<\/code> sorts on natively, so no extra query code is needed.<\/li>\n\n\n<li>The cell prints <code>absint( $post_id )<\/code>, and the header text is escaped and translatable.<\/li>\n\n\n<li>A one-line style on list screens keeps the column narrow.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The older One-Click Import version declared its functions inside a closure, which fatals with &#8220;Cannot redeclare&#8221; if that closure ever runs twice, used unprefixed names and was not sortable. This version declares top-level <code>dpc_<\/code> functions and only hooks post types that have an admin list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Admin only<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty. Admin only code is loaded on wp-admin requests only, which is exactly where the column lives, and the hooks run later on <code>admin_init<\/code>, when post types are registered.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ID first, right after the checkbox: in <code>dpc_add_id_column()<\/code> build a new array with <code>cb<\/code>, then <code>dpc_id<\/code>, then the remaining columns.<\/li>\n\n\n<li>Only some post types: replace <code>get_post_types( array( 'show_ui' =&gt; true ) )<\/code> with a list such as <code>array( 'post', 'page', 'product' )<\/code>.<\/li>\n\n\n<li>Media Library list view: it uses <code>manage_media_columns<\/code> and <code>manage_media_custom_column<\/code>; hook <code>dpc_add_id_column<\/code> and <code>dpc_render_id_column<\/code> to those as well.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is the sortable successor of <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/page-post-and-cpt-id-in-admin-columns\/\">Page, Post, and CPT ID in Admin columns<\/a>. For more list columns see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/show-featured-image-in-admin-posts-and-pages-table\/\">Show featured image in admin posts and pages table<\/a> and <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/add-taxonomy-to-admin-columns\/\">Add Taxonomy to Admin columns<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each user can hide the column under <strong>Screen Options<\/strong> on any list screen.<\/li>\n\n\n<li>WooCommerce orders with HPOS use their own list screen, not a post type list, so they don&#8217;t get the column; the order number there is already the ID.<\/li>\n\n\n<li>A post type registered after <code>admin_init<\/code> (rare) won&#8217;t get the column.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Show the post ID in a sortable column on every WordPress admin list, posts, pages and custom post types, with one Admin only snippet.<\/p>\n","protected":false},"author":1,"featured_media":46632,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Add a Sortable ID Column to WordPress Admin Lists","description":"Show the post ID in a sortable column on every WordPress admin list, posts, pages and custom post types, with one Admin only snippet."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[177,141,178,166,6],"dpc_library":[204],"class_list":["post-46631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-admin-columns","tag-custom-post-types","tag-post-id","tag-scripts-organizer","tag-wp-admin"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46631"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46631\/revisions"}],"predecessor-version":[{"id":46664,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46631\/revisions\/46664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46632"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46631"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46631"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}