Add id (custom post types) column to page and posts list table in wordpress and make them sortable


WordPress doesn’t show the ID in the page and post table lists by default. To see the id, you have to mouseover the title links which is time consuming. The same method applies for custom post types. I hope wordpress makes it easier to do in the future.

// Add id to page and post and make them sortable
add_filter('manage_posts_columns', 'my_columns_id', 10);
add_action('manage_posts_custom_column', 'my_custom_id_columns', 10, 2);
add_filter('manage_pages_columns', 'my_columns_id', 10);
add_action('manage_pages_custom_column', 'my_custom_id_columns', 10, 2);
add_filter('manage_edit-page_sortable_columns', 'my_custom_id_sortable');
add_filter('manage_edit-post_sortable_columns', 'my_custom_id_sortable');
add_action('admin_head', 'my_custom_id_styling');

function my_columns_id($defaults){
  $new = array();
  $count = 0;
  foreach ($defaults as $k => $v) {
    if ($count == 1) {
      $new['wps_post_id'] = __('ID');
    }
    $new[$k] = $v;
    $count++;
  }
  $defaults = $new;
  return $defaults;
}

function my_custom_id_columns($column_name, $id){
  if($column_name === 'wps_post_id')
    echo $id;
}

function my_custom_id_sortable($col) {
    $col['wps_post_id'] = 'ID';
    return $col;
}

function my_custom_id_styling() {
  echo '<style type="text/css">';
  echo 'th#wps_post_id{width:50px;}';
  echo '</style>';
}
Like it.? Share it:
Tags:

Comments are closed.