How to Get a WordPress Post ID (Code Snippets)

The post ID in WordPress can be used for tons of reasons. Whether it’s for debugging or custom code here are 5 ways to get WordPress post ID.

Find Post ID in WordPress Dashboard

The easiest way to get the ID of a post in WordPress is to login to your site and edit a post. While editing a post in WordPress you can see it’s post ID in the URL of your browser.

This can be done by heading to Posts/All posts.

View all posts in WordPress

Here you can click “edit” on the post you want to see the post ID of.

After selecting a post you’ll be able to see the post ID in the browser URL bar.

View Post IDs using WordPress Plugin

If you just want to see your post IDs in your WordPress dashboard as a column you can use the plugin Reveal IDs.

After enabling the plugin you’ll see an ID column in your WordPress dashboard for all post types.

Post ID column in the dashboard

Get Current Post ID (using PHP)

Want to get the current post ID of the current post? Using the get_the_ID() function will easily get post ID. This works in template files or inside any loop.

echo 'The current post ID is: '.get_the_ID();

Get Post ID by Slug (using PHP)

The get_page_by_path() function allows you to get post data in WordPress using the post slug. So in this example we will be getting the post ID from its slug.

$post_data = get_page_by_path('my-post-slug');
$post_id = $post_data->ID;
echo 'The post ID is: '.$post_id;
Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 10 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way.

Leave a Reply

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

WordPress Tips Monthly

Get the latest from SmartWP to your inbox.