WordPress Get Post ID

Updated on November 09, 2017

There will come a time while using WordPress that you might need to get the Post ID, either for a plugin or perhaps you are a developer and need to query it. See the directions below on how to easily find the Post ID from within your WordPress dashboard and also with code.

wordpress get post id

WordPress Get Post ID

Just like with your SSN, each page, post, and or custom post type in WordPress all have their own unique numeric post ID assigned to them in the database to identify them from one another. Behind the scenes, the post IDs are used by queries for all sorts of things.

How to Find The Post ID

When working with different WordPress themes and plugins, there will be times where they might ask for the Post ID. Usually these are used to filter posts and or exclude them.  For example, the free Display Widgets plugin allows you to hide widgets on certain pages or posts based on categories, tags, etc. It also has an option to hide the widget based on comma separated list of post IDs.

comma separated by post id

To find the post ID you can click into the posts section of your WordPress dashboard and hover over the title of a post. In the bottom of your browser you will see the post ID. In this example below you can see the post ID is 5908.

wordpress get post id

Another way to find it is to edit your post and the post ID will appear in your browser’s address bar. This same thing applies for pages, custom post types, etc.

post id address bar

Show Post IDs in WordPress Dashboard

When WordPress 2.5 was released, Post IDs were no longer shown in the dashboard. However, if you work with Post IDs a lot, there are some easy easy to re-enable that functionality. The first way to is to simply use a free plugin called Show IDs, developed by 99 Robots.

wordpress plugin show ids

This plugin is super lightweight, only 3 KB to be exact, and doesn’t make any front-end calls. As of writing this, it currently has over 10,000 active installs with a 5 out of 5 star rating. You can download it from the WordPress repository or by searching for it within your WordPress dashboard under “Add New” plugins. There is nothing to configure, simply install, activate, and Post IDs will show in your dashboard. It also supports page IDs, category IDs, tag IDs, taxonomy IDs, etc. Below is an example of what you would then see.

show post id in wordpress dashboard

And if you don’t want to install another plugin, our friend Daniel Pataki also wrote a great tutorial on how to enable Post IDs in your WordPress dashboard with code.

Get Post ID Function

If you are a theme or plugin developer and are needing to query the Post ID, you can use the following functions. Use the following code within a loop to display the numeric ID of the current post.

<?php the_ID(); ?>

If you are working outside of the loop, say in the footer. You cannot use the above function to get the post ID. Instead you have to use the following:

$post->ID

Example:

function function_name() {
global $post;
$thePostID = $post->ID;
}
Was this article helpful?
No, or there was something off

Hand-picked related articles

Send this to a friend