Home / Add ID column in admin tables
Duplicate Snippet

Embed Snippet on Your Site

Add ID column in admin tables

Display the post id in the admin in its own column for convenience.

200+
Code Preview
php
<?php
add_action( 'admin_init', function () {
// Get all public post types
	$post_types = get_post_types( array(), 'names' );
	function wpcode_add_post_id_column( $columns ) {
		$columns['wpcode_post_id'] = 'ID'; // 'ID' is the column title
		return $columns;
	}
	function wpcode_show_post_id_column_data( $column, $post_id ) {
		if ( 'wpcode_post_id' === $column ) {
			echo '<code>' . absint( $post_id ) . '</code>';
		}
	}
	foreach ( $post_types as $post_type ) {
		// Add new column to the posts list
		add_filter( "manage_{$post_type}_posts_columns", 'wpcode_add_post_id_column' );
		// Fill the new column with the post ID
		add_action( "manage_{$post_type}_posts_custom_column", 'wpcode_show_post_id_column_data', 10, 2 );
	}
} );

Comments

Add a Comment