How to Display Post and Page IDs in the WordPress Admin
Every now and again it would be convenient to know the ID of a post or page in WordPress, right?
Whether it’s for a shortcode, when setting something up in the theme settings, or maybe just to get a quick link.
Whatever the case may be, WordPress doesn’t make it easy to figure out a post’s ID. One way to grab it is to visit the edit page of a post and check out the URL. It should look something like this:
The 137171 in the example above is the ID of the post – the main identifier in the database. Surely there must be an easier way to figure it out right? Fortunately, there is.
Display Post IDs with a Plugin
The easiest solution is to use a plugin. The granddaddy is Reveal IDs, a free plugin that clocks in at just over 425,000 downloads.
Another new option is WPsite Show IDs.
Both plugins do just about the same thing: show the ID of posts, pages, users, categories, custom taxonomies, custom post types and so on. The only reason I lean toward WPsite Show IDs is the 8Kb footprint. Reveal Ids is around 311Kb, which seems a bit excessive for such a simple plugin.
DIY: Display Post IDs with Code
If you’re interested in how to display post IDs yourself, let’s take a look at the code.
The code below should go inside a plugin or your theme’s functions file. If you would like to create a plugin, take a look at our guide to plugin development for a simple template.
Before we get started, it’s also worth saying you should create a child theme. Check our our guide to child themes if you’re not already familiar with how to create one.
Adding Custom Columns
WordPress offers great tools to modify admin post lists, including creating your own columns and content. We’ll need to use a filter to add the column and an action to add the values. Let’s do a quick test on the regular posts table:
That is all we need. The filter allows us to add a column by modifying the columns array. The array key should be the identifier for the column and the value will be displayed as the header text.
The function hooked to the action takes two parameters: the column name and the id of the post being shown. This is perfect – we make sure to simply echo the ID when our custom column is shown.
The “revealid” function is my attempt at a pun, sorry about that! It is meant as a prefix for all our functions to make sure they don’t collide with other plugins.
A quick aside: Note how I used
'revealid_id' == $column, which seems a bit alien. This is called a Yoda condition and is preferred in WordPress. The reasoning is that if you forget to define the variable you won’t get a huge gaping PHP error on your page.
Here’s what it looks like in the backend of WordPress:
Finding the Right Hooks
The two functions above are all we will need. The remaining piece of the puzzle is where to hook them in. The hooks we used target regular posts only and our IDs will not show up for pages or other elements.
In reality, these hooks are called variable hooks because they belong to a standardized set. The common form is:
Based on this, posts, pages, media and custom post types should be easy, since these are all post types. To make IDs show up for all these elements you can use the following hooks:
Small caveat here: Everything except posts and pages uses the post types. The post type for posts is technically “post,” the post type for pages is “page.” For some reason the hooks use the plural form. This is a WordPress quirk since it really should be the singular form. See, even the WordPress core isn’t perfect.
I’ve added a custom post type in there: project. What if you want to apply this to all custom post types? In that case you can cycle through all of them easily like so:
I recommend the same approach when dealing with taxonomies. To output the ID for categories only you could use
manage_link_categories_custom_column but to add it to all taxonomies we need to use another loop:
Last but not least we have users and comments. These are fairly straightforward as well. Take a look:
Placing the ID in Front
This small change is a bit more difficult than it seems. When we added the ID column we appended it to the end of an existing array containing all the other columns. The solution seems easy enough: Add it to the front. We could do this by merging arrays but it turns out that the checkbox is the first column – we actually want the ID in second place.
We’ll still use array merge but we need a bit more trickery – we need to split it up first. The first array will contain the checkbox (the first element of the original array), the second array will contain everything else. We will merge the first array with an array containing our ID and then with the second array. The code should make this a lot clearer:
I think this is a great example of the modularity of WordPress. IDs were once shown in the admin (pre-WordPress 2.5) but it turned out not many people needed them. Once this feature was removed, plugins sprang up to cater to those who still wanted to see the post IDs.
Writing our own plugin provides a glimpse of how modular WordPress is and how easy it is to modify the admin itself. The same method outlined above could be used to add thumbnails, description snippets and other information to the admin list table.
If you’ve added something awesome to an admin list, or have an idea you’d like to see added to it, do let us know in the comments below.
WIN a Share of $5K
Subscribe to our blog this #hostingmonth for a chance to win one of 5 prizes of $1,000 WPMU Dev credit! Learn More.