16 Ways to Get Post ID in WordPress
Are you looking for ways to get the WordPress post ID but you don’t know where you can find it and use it in your functionality or whatever you need for.
There could be many reasons you need a post or page ID and you don’t know how to get it and where you can see it. I also sometimes search on google to find a suitable way to find the user ID for my custom functionality.
Don’t worry, you are in right place.
In this tutorial, I will show you many ways to get a post ID or page ID in WordPress. We will cover all methods that we will have to use to get WordPress post ID, like using a plugin, from the WordPress dashboard and with the help of coding.
So let’s get started.
Get Post or Page ID in WordPress
To get the post or page ID in WordPress is very easy and you can get it by following the simple methods. You will see many ways to get the post ID in WordPress in this tutorial guide, and then you can perform your action based on it.
That all methods work for WordPress Posts, Pages, and also custom post types.
1. Find Post or Page ID in WordPress Dashboard
In this simple and very easy method, you have to login into the WordPress dashboard. Then go to Posts > All Posts and then click on Edit option of any post.
When you click on the Edit option then the post will be open in edit mode and you will see the post ID of that post in the URL as a query string value. See the following image for reference.
2. Find Post ID by Hovering Post
This method is the same as above but you just have to hover the post edit option instead of clicking on it. When you hover over the Edit option, you will see the post ID or page ID at the bottom of the windows.
So, navigate the Posts > All Posts option from the left sidebar and then mouse hovers over any post for that you want to get post ID. See the following image as a reference.
3. Get Post ID From Global $post Object
In this method, we will use the global $post
object to get the post ID in WordPress. This global $post
object has all the data information about the current post.
Like you can get the post title, post slug, post description, post taxonomy, post publish date, post update date, any parent post ID, etc.
Note: Make sure to add the global $post
variable at the beginning of the script.
4. Get Post ID Using get_the_ID() and the_ID()
You can use the get_the_ID()
and the_ID()
functions to get the post ID or page ID in your code.
The difference between these two functions is that the get_the_ID()
function returns the post ID, and the_ID()
function print the post ID.
5. Get Post ID by Title
In this method, we will get the post ID by post title, so you just have the post title. This is a very easy method and you can use this in any template file or functions.php file.
So we will use WordPress built-in function get_page_by_title()
function. You have to pass the post type as the third parameter. The default parameter is ‘page‘, so if you want to get the post ID then you need to pass the ‘post‘ as a parameter.
You can also use it for the custom post type, just pass the custom post type name parameter.
6. Get Post ID by Slug
The same way as above just has a different function name. Use the get_page_by_path()
function to get the post or page ID by a slug of it. Also can use for pages and custom post types.
7. Get Post ID by URL
Use the url_to_postid()
function to get the post ID by post URL. Pass the post URL as a parameter in this function and it will return the post ID.
8. Get Post ID from WP_Query Loop
Using the WP_Query
WordPress Query class is the fastest way to get the post ID. It will return the $post
object and you can easily get any information of post through the loop.
You have to make a new instance of query class by passing the query arguments and then using the have_posts()
function in the while loop to check the posts. Then you can get the post ID in the loop.
As in the above code, we used the $post
object property and get the post ID but you can also use the get_the_ID()
function as I commented out.
9. Get Post ID by Metadata
In this method, we will use the post meta key and meta value to get the post ID. We will make a SQL query using the $wpdb->get_results()
function that will return the post ID based on the matched meta key and meta value.
You can also get the other information about the post if you want to get it. So see the following reference code to get the post ID by meta key and meta value.
You can also use the get_posts()
function to get the result from the database with meta query.
10. Get Post ID by Term ID
If you only know the term ID (category ID or tag ID) and want to get the post ID that belongs to the term ID then use the get_posts()
function.
It will give you an object and you can loop through and get the post ID. You can also use the $wpdb
query to get the result from the database.
11. Get Post ID from WooComerce Order
If you are using the woo-commerce platform and you have an order number and you want to get the product ID (post ID) from it then look at the following code as a reference.
First, we will get order detail using WC_Order()
function and get the items from it. Then loop through the items and get the product ID.
12. Get Post ID from phpMyAdmin
This method is for advanced developers, who are familiar with phpMyAdmin and know how to access the database to get details.
So simply go to phpMyAdmin and then select the database and then wp_posts table and showing in the following image. Here you can run the SQL query to filter the users.
13. Get Post ID by Adding Custom Column
In this method, we will add a new column in the Posts list in the dashboard area. We will use WordPress action hooks to add the new column at end of all existing columns.
First, we will register the custom column and then show the value of that column as a post ID. You can do more customization according to your requirements.
Add the above code in your child theme’s functions.php file and save it. Now go to All Posts > Posts and you will new custom column appearing with a value showing of post ID.
14. Get Post ID by Plugin
This method is the alternative to the above method. If you don’t much familiar with the WordPress action hooks or custom functionality then use this simple and easy method.
So in this method, we will use the Reveal IDs plugin. So, install and activate the plugin and then navigate to Posts list under All Posts, you will see a new column with name ID showing and also its value as post ID.
15. Get Post ID by Plain Permalink Structure Settings
If your website has set up a plain permalink structure setting then you will see the post ID in the permalink (URL) at the frontend.
Go to Settings > Permalinks and then check, Is it permalink set up for Plain under Common Settings? See the following image for reference.
If it is a Plain setting then you will see your all pages and posts permalink has an ID in it. You can view it at the front end.
16. Get Post ID by Inspect Element at Frontend
This is the method for those who know the browser’s developer tool panel. Because in this method we will use the browser’s developer tool panel to find the WordPress post ID.
So open your post at the frontend and right-click on the post title or anywhere then you will see the browser’s option, select “Inspect“. It will open the developer panel.
You can also use the shortcut keys to open the developer panel. Press CTRL+SHIFT+I, it will open the developer tool panel. If this not working for you then you can go through browsers settings and find the developer tool option.
When opened the developer tool panel, find the <article>
HTML tag inside HTML structure. You will see the <article>
tag has an id attribute with a value of post ID. So you can use it. See the following image for reference.
That’s it!!!
Conclusion
So in this tutorial, you learned about how to find the WordPress post ID in the dashboard area and how you can get it programmatically way in your function.
So you learned 16 ways to get the WordPress post ID. We used a plugin and we see it on the admin side in the dashboard area. And if you know the coding then you can check the programmatic ways. All methods are very simple and you can easily get the post ID.
So if you still have any questions or suggestions then don’t hesitate to ask. Please do let me know in the comment section, I will respond to you as soon as possible.
See Also: 10 Ways to Get User ID in WordPress