You’ll need to identify an ID for a page every once in a while on a WordPress website. If you aren’t sure what it is, it’s a unique identifying number that corresponds to a specific item in your WordPress database, this time a page. The way it works is kind of obvious – you’ll point to it when enabling or disabling features or showing/hiding elements on certain pages. In practice, this can happen while customizing the WordPress themes you installed, or even a plugin, code snippet, or a function inside your functions.php file. In the end, the specifics are irrelevant. Once we teach you how to get page ID in WordPress, you can utilize it for whichever purpose you want.
1. Check the URL to get the page ID
This is by far the simplest method, and we recommend it highly. Follow these steps to get page ID in WordPress:
- Access the WordPress Admin Section.
- In the main WordPress menu on the left, click on Pages > All Pages.
- If the page is recent, find it in the list. If not, use the search feature in the top right corner.
- There are two ways to check page ID now:
- Check the URL in the address bar at the top of the browser window. The page ID is the number between “post=” and “&action”. For example ?post=256&action…
- Hover over the name of the page with your cursor. Look at the URL in the bottom left corner. The same instructions apply.
2. Use a WordPress plugin to get the page ID
Although method 1 works without any additional functionality, sometimes you need to see IDs on a whole list of pages at once. You can add this functionality with a code snippet, but it’s much riskier and might break your site eventually. Instead, we recommend you install a WordPress plugin dedicated to this functionality. A good example is Reveal IDs because it’s lightweight and only does exactly what the name states. IDs will appear in a separate column (usually 5th or 6th) in Pages > All Pages after you activate it.
3. Find the page ID inside your WordPress database
Your WordPress database serves the content when requested. For that reason, the page ID can be found inside. You can browse databases in WordPress via a dedicated program, such as NavicatSQL, or better yet, through the cPanel on your hosting website. Simply log in, and then:
- In the cPanel, find the Databases section, and click on an app you prefer. We’ll use phpMyAdmin.
- Once it opens, expand the list of tables in the WordPress database in the sidebar on the left.
- Find an entry named wp_posts.
- Clarification. There is no wp_pages database. Both posts and pages are consolidated inside one database and separated by post_type.
- Look for your page title under the post_title column. You can also use the search feature.
- When you find it, simply check the ID column that corresponds to the title. It’s usually the first column after Edit, Copy, and Delete.
4. Using PHP functions to get the page ID in WordPress
Before we start, we should remind you what WordPress Loop is. To put it simply, it’s the PHP code that is responsible for displaying WordPress posts and pages and their elements on a web page. With that said, some of these quick codes are designed for utilizing page ID inside of it, while others aren’t (for example, widgets). Depending on what you’ll use page ID for, here are a few examples of the way to identify page ID:
1. Within WordPress Loop
Here are 3 ways to obtain page ID inside WordPress Loop:
$postid = get_the_ID();
- To display it on the front end, you can use this too:
<p>Post Number: <?php the_ID(); ?></p>
$id = get_queried_object_id();
2. Outside of WordPress Loop
To fetch the page ID outside of the WordPress loop, try one of these 6 techniques:
- Search by page title:
$mypost = get_page_by_title( 'Enter page title here', '', 'page' );
- Search by page slug:
$mypost = get_page_by_path('replace-with-page-slug', '', 'page');
$page_id = get_queried_object_id();
- We recommend this line of code if you need to add page ID to widgets:
$post_obj = $wp_query->get_queried_object();
$Page_ID = $post_obj->ID;
- Another simple solution if you plan to use this functionality often. Add this to your functions.php file:
$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
Then, whenever you need to call the function, use this in the template:
get_page_id('replace with page name');