WordPress generates a unique number for every content on your website so that you can quickly identify them. This includes posts, pages, and custom post types.
Post ID is often required by certain plugins, code snippets, or WordPress itself when customizing specific content.
In this article, you will learn how to get a post ID in WordPress using two methods — PHP code and manual steps. Let’s get started.
How to Get WordPress Post ID
For the first method, you will need to have some technical knowledge. If you’re new to website development, you might want to try getting the post ID manually.
Using PHP Code
If you’re coding a theme or creating a plugin for your WordPress site, there are times when you need to use this method of querying the post ID.
In the single.php file, you can insert the_ID(); within the loop, right after the <?php tag. This function will print the post ID to the front end of your website. You will see the post ID number on the left of the page, under the header when you view your posts.
If you’re editing outside of the loop, like in the header.php or footer.php, there are a few ways you can retrieve the post ID.
- Insert the following code under the first <?php and update the file:
- If it didn’t work, use:
global $post; echo $post->ID;
- Or, place the get_the_ID(); function instead, to retrieve the ID of the current post as a variable.
- If you want to just display the post ID, using the_id(); is enough.
- To show the ID of a specific post, you can use its title through the following code:
$variable = get_page_by_title(‘Title Goes Here’, '', 'post'); echo $variable->ID;
- The post’s slug can also be used:
$variable = get_page_by_path(‘post-slug’, '', 'post'); echo $variable->ID;
- You can also use the permalink or URL of your post to get its ID with the following code:
$variable = url_to_postid(‘URL Goes Here’); echo $variable
How to Get WordPress Post ID Manually
Alternatively, there are three ways in which you can get the post ID manually — in the post’s URL, by using a plugin, and through the WordPress database.
Find Post ID in the Post URL
As an admin, this is the easiest way to get a post ID:
- Access your WordPress dashboard and select All Posts under the Posts menu. Click on a post of your choice.
- Look up at the URL bar, and you will find the post ID numbers in-between “post=” and “&action”.
Get Post ID with a WordPress Plugin
If you require frequent access to post IDs, then you might prefer using a plugin.
The first option is Catch IDs. With this plugin, you will see a new ID column to the left of the Title column on the All Posts page.
As an alternative, there’s Reveal IDs. It has the same results as the previous plugin, but the ID column will be to the right of the Date column.
If you want to know the post IDs for post types and taxonomies, then use Show IDs. Just like the previous plugin, you’ll see the post IDs in the far right column.
Find Post ID in the WordPress Database
This method is a bit impractical compared to the others, but you can still give it a try:
- Access your hPanel, scroll down, and navigate to phpMyAdmin under the Databases section.
- Go through your databases by pressing the Enter phpMyAdmin button.
- Find the wp_posts table and click on it. You will see the ID column to the left of the post_author column.
Post ID is the unique identification number of your WordPress website’s posts. You can retrieve them by using the PHP code given above if you’re currently customizing your website theme or a plugin.
For beginners, there are easier ways in which you can get the post ID. You can find it in the post’s URL, by installing plugins like Catch IDs or Reveal IDs, and through your WordPress database.