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.
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.
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.
<?php | |
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.
<?php | |
$post_data = get_page_by_path('my-post-slug'); | |
$post_id = $post_data->ID; | |
if(!empty($post_id)){ | |
echo 'The post ID is: '.$post_id; | |
} |