How to find and display an ID in WordPress

When you start writing your new blog, it doesn’t really matter what’s your post, page, category or tag ID, does it. You’ll have only a few of each and you will be able to count them by yourself or find a post ID written in the URL, for example.

Before WordPress version 2.5, an ID was shown in the admin area by default but developers chose to remove the ID column since not many people needed one.

So, if you’re working on a bigger WordPress site or even developing one, you will have to find a different method of finding and displaying those IDs.

In this article, we’re about to show you how to add a post ID number right next to your date in the dashboard. After you place the following code into your theme’s functions file or into you site-specific plugin, IDs become visible easily and you won’t be losing time finding ones in each and every post.

Of course, there are plugins available which will do all this without forcing you to write code.

Add ID for posts:

  1. Open you functions.php or your site-specific plugin file
  2. Copy and paste the following code:
  3. add_filter( 'manage_posts_columns', 'revealid_add_id_column', 5 );
    add_action( 'manage_posts_custom_column', 'revealid_id_column_content', 5, 2 );
    function revealid_add_id_column( $columns ) {
    $columns['revealid_id'] = 'ID';
    return $columns;
    }
    
    function revealid_id_column_content( $column, $id ) {
    if( 'revealid_id' == $column ) {
    echo $id;
    }
    }

    This piece of code works only for your regular post types. To display IDs for posts, pages and media, you will need to add the following code to your functions.php file or your site-specific plugin file.

    Add ID for posts, pages, media:

    add_filter( 'manage_posts_columns', 'revealid_add_id_column' );
    add_action( 'manage_posts_custom_column', 'revealid_id_column_content' );
    
    add_filter( 'manage_pages_columns', 'revealid_add_id_column' );
    add_action( 'manage_pages_custom_column', 'revealid_id_column_content' );
    
    add_filter( 'manage_media_columns', 'revealid_add_id_column' );
    add_action( 'manage_media_custom_column', 'revealid_id_column_content' );

    Add ID for users and comments:

    add_action( 'manage_users_columns', 'revealid_add_id_column' );
    add_filter( 'manage_users_custom_column', 'revealid_id_column_content' );
    
    add_action( 'manage_edit-comments_columns', 'revealid_add_id_column' );
    add_action( 'manage_comments_custom_column', 'revealid_id_column_content' );

    If you want to show ID for your taxonomies, you should use the following code if you don’t want to add a taxonomy one by one.

    Taxonomies:

    $taxonomies = get_taxonomies();
    foreach ( $taxonomies as $taxonomy ) {
    add_action( 'manage_edit-' . $taxonomy . '_columns', 'revealid_add_id_column' );
    add_filter( 'manage_' . $taxonomy . '_custom_column', 'revealid_id_column_content' );
    }

    What about custom post types? You can do something similar. Instead of adding each and every custom post type you have, you can simply give them all an ID column.

    Custom post types:

    $custom_post_types = get_post_types(
    array(
    'public' => true,
    '_builtin' => false
    ),
    'names'
    );
    foreach ( $custom_post_types as $post_type ) {
    add_action( 'manage_edit-'. $post_type . '_columns', 'revealid_add_id_column' );
    add_filter( 'manage_'. $post_type . '_custom_column', 'revealid_id_column_content' );
    }
  4. Save changes and you’re done

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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