Are you looking to build a custom plugin or function for some specific reasons in WordPress and want to get the post or page IDs? In WordPress, every post or page has a unique identifier known as an ID, and these IDs are the ones that differentiate one post or page from another.
And while you’re writing the codes, you often need to call them for specific reasons, like displaying relevant posts on your website or showing the meta information for XYZ reasons. So, in this case, what you do is you need to call those posts and pages by their IDs.
Depending upon the reason you’ve, here in this guide, we’ll explore different methods for getting the post and page IDs. These methods will include all options, including options to find post or page IDs for one-time usage to writing custom codes.
So, let’s get started…
How to Get Post & Page ID in WordPress
In order to make it easier for you to locate posts and page IDs, we have grouped them into three major categories. These categories are;
- Manually Finding Post and Page IDs
- Using the “Reveal IDs” WordPress Plugin
- Writing Codes for Different Use Cases
Let’s start with the easy one…
1. Manually Finding Post and Page IDs
If you’re looking to find the IDs of just a few posts or pages in WordPress, then manually finding or retrieving them is undoubtedly one of the easiest ways for you.
In this method, you simply need to locate the page or post and click on the “Edit” button and retrieve the ID from the URL box.
Here’s the step-by-step guide describing the same;
Let’s suppose I wanted to get the post ID of one of my favorite posts, which is “Speeding Up WooCommerce Website“, then what I’ll do is first I’ll search for this post in the “All Posts” section. Once I find this post, I’ll hover on it and then click on the “Edit” option (as shown in the image above).
After that, I need to look at the URL of the page in the browser’s address bar. And as you can see in the image above, the URL is “https://thewpx.com/wp-admin/post.php?post=6910&action=edit“.
Here the ID of the post is the number after “post=” in the URL, which in my case, is 6910. Similarly, when you visit any post or page on your website, you’ll be able to see the post or page ID.
2. Using the “Reveal IDs” WordPress Plugin
Another easiest and most convenient method for getting a post or page ID in WordPress is by using the “Reveal ID” plugin. This is one of the best plugins in the WordPress repository for finding the IDs of posts, pages, and other WordPress IDs.
Here’s how to find posts and page IDs using this plugin;
First of all, you need to download and install this plugin on your website (if you’re not sure how to install plugins on your website, you can follow this guide).
Once the plugin is installed and activated on your website, you need to visit the “All Posts” or “All Pages” section of your website.
Here I’m looking for the ID of my favorite post, so I’ll visit the “All Posts” section and search for it. And as you can see in the image above, a new column named “ID” has been added to my posts containing the post’s ID.
So, now I can easily find the IDs without going through any trouble.
3. Writing Codes for Different Use Cases
Till now, we’ve discussed the options to manually find and use them on some static functions – but what if we’re building something advanced and we need to dynamically fetch the IDs of posts and pages?
What should we do here?
So, in this case, we need to write custom code based on our requirements and possibilities. Here we’re sharing a few methods for grabbing the IDs of posts and pages depending on our requirements.
3.1 Getting ID of Current Page
If you are looking to find the ID of a specific page that you are working on, then using the get_the_ID() function is the best way to do so. This is a pre-built WordPress function that is used to retrieve the ID of the current item in the WordPress loop.
Here’s the dummy code for reference;
When executed, this code retrieves the ID of the current page or post in WordPress and prints it on the screen.
3.2 Getting Post ID by Slug
The code that we shared above is only executed when users are on the page and not when they are not. But what if you needed to get the ID of the page more dynamically?
In this case, we used a pre-built function in WordPress named get_page_by_path(). This function retrieves the page object from the database based on the page’s path (slug).
Here’s a code for your reference;
This function simply searches for the page with the slug “my-post-slug” to retrieve its object. If the post is found, it simply prints the ID followed by the text, and if no posts are found, it prints an error message.
3.3 Getting Post ID by Title
In case you’re looking to get the post or page ID from its title, then you can use another incredible function named get_page_by_title(). It is also a pre-built WordPress function that takes multiple inputs from you and retrieves page or post OBJECT from the database.
Here’s a reference code;
3.4 Getting Post ID by Full URL
Till now, we’ve covered different types of functions to get the post or page ID in WordPress, and the last one we’re going to cover in this article is url_to_postid(). It is also a built-in function like others we’ve covered in this guide – but it takes the full URL as the input and gives the ID.
Here’s a reference code for using it;
In the code above, we’ve shared a full URL (https://thewpx.com/hello-world/) instead of providing just a slug (hello-world) and used an if condition to retrieve the response.
Based on your requirements, you can make changes to these codes and use them as per your needs.
If you’re building a plugin or custom function for your website, then there are several methods for getting the post IDs in WordPress. And if you’re someone who’s not building dynamic plugin, method one and two works fine for you.
But if you’re working with more complex codes and wanted to find the post IDs dynamically, you can try the built-in functions we’ve shared in method 3. And regardless of the method you choose, its always better to know how things work in WordPress.
I hope this article has been helpful in showing you how to get the post and page IDs. And if you have any questions or need further assistance, don’t hesitate to leave a comment below.