NAVI

How to find post, page, tag, category and media ID in WordPress

article header image

While I’ve loved working with the superflexible WordPress CMS for years now, it too has its shortcomings :-). One of them is that you cannot see the ID for pages, posts, media and categories unless you hover over the title on the overview pages in WP-admin. Very unpractical if you’re a developer and need to look at them regularly during build. I’m sure the developers at WordPress are aware of it, so it must be a conscious descision. Not one they made thinking of other designers!

Luckily, there is a simple solution, which I found on ! By adding the code below to your themes functions.php file, you will have the following extra columns in WP-admin:

  • Posts overview: ID
  • Categories overview: ID
  • Pages overview: ID
  • Media library: ID and image dimensions

Here comes the code (it does not matter where in the functions file you paste it as long as it is between php open and close brackets)

<?php
// Add ID's to posts and pages overviews
add_filter('manage_posts_columns', 'posts_columns_id', 5);
add_action('manage_posts_custom_column', 'posts_custom_id_columns', 5, 2);
add_filter('manage_pages_columns', 'posts_columns_id', 5);
add_action('manage_pages_custom_column', 'posts_custom_id_columns', 5, 2);

function posts_columns_id($defaults){
    $defaults['wps_post_id'] = __('ID');
    return $defaults;
}
function posts_custom_id_columns($column_name, $id){
        if($column_name === 'wps_post_id'){
                echo $id;
    }
}

// Determine ID column width
add_action('admin_head', 'custom_admin_styling');
function custom_admin_styling() {
echo '<style type="text/css">';
echo 'th#wps_post_id{width:50px;}';
echo '</style>';
}

// Add ID to categories overview
function categoriesColumnsHeader($columns) {
        $columns['catID'] = __('ID');
        return $columns;
}

add_filter( 'manage_categories_columns', 'categoriesColumnsHeader' );

function categoriesColumnsRow($argument, $columnName, $categoryID){
        if($columnName == 'catID'){
                return $categoryID;
        }
}

add_filter( 'manage_categories_custom_column', 'categoriesColumnsRow', 10, 3);


// Show media ID in media library overview
function column_id($columns) {
    $columns['colID'] = __('ID');
    return $columns;
}
add_filter( 'manage_media_columns', 'column_id' );
function column_id_row($columnName, $columnID){
    if($columnName == 'colID'){
       echo $columnID;
    }
}
add_filter( 'manage_media_custom_column', 'column_id_row', 10, 2 );


// Show image dimensions in media library overview
function wh_column( $cols ) {
        $cols["dimensions"] = "Dimensions (w, h)";
        return $cols;
}
add_filter( 'manage_media_columns', 'wh_column' );
function wh_value( $column_name, $id ) {
    if ( $column_name == "dimensions" ):
    $meta = wp_get_attachment_metadata($id);
           if(isset($meta['width']))
           echo $meta['width'].' x '.$meta['height'];
    endif;
}
add_action( 'manage_media_custom_column', 'wh_value', 11, 3 );
?>

foto Boris Hoekmeijer
My name is Boris Hoekmeijer, I'm a webdesigner and graphic designer.
I sometimes run into a problem for which I have to find my own solution because Google just won't tell me. That's when I climb behind my mechanical keyboard, and fire away until a tutorial materializes. One of the great things about the web: we can all help each other!
If this article has helped you, or if you have anything to add or ask, please leave a comment or share the post.
Cheers!

© 2014 ★ Published: August 8, 2014
1 Comment

  • Kapil Garg says:

    Thanks for sharing this valuable post. I hope You Will post more knowledgeable post for us.

Leave a Reply

Your email address will not be published. Required fields are marked *