How do you find your WordPress page ID and post ID? There are several situations where you may need to determine the ID of a specific post or page.
An example of this might be a plugin that needs to exclude that page from a feature. However, if you are a developer, you might have to query that ID to do this.
You can identify each piece of content on your website by its ID number, assigned by WordPress to each page and post.
Traditionally, the platform does not display this information as openly as expected. Still, if you know where to look, it is pretty straightforward to locate. Getting post IDs in WordPress can be accomplished in several different ways.
Throughout this article, we will discuss what WordPress post and page IDs are and various methods for getting post and page IDs in WordPress.
What Are WordPress Post And Page IDs?
Each content in WordPress has its numerical ID, including posts, pages, media attachments, categories, tags, and custom post types.
Suppose WordPress needs to serve up a piece of content. In that case, it queries the database using the post ID to find the content and associated metadata.
Having this unique identifier is essential since it is static. Post titles and slugs may change, but post and page IDs will never change. So, you cannot change page ID in WordPress.
This is why plugins that need to include or exclude specific posts, such as a related posts plugin or a social sharing plugin, will commonly ask for a particular post ID.
In some cases, it is also necessary to enter WordPress post IDs to build custom shortcodes. Whenever you use a shortcode requiring you to specify a specific post, you must provide that post’s ID.
A further advanced example is when adding custom code to your website. Still, you wish to only run it on specific pages.
In this scenario, you can tell WordPress that the code should be run if the ID of a post or page matches the IDs you specify.
How to Get Post and Page IDs in WordPress?
It is relatively simple to locate the ID for a WordPress page or post, although it may initially seem complicated.
There are several methods by which you can discover a post or page’s ID in WordPress, regardless of your scenario.
In this section, we will present some of these methods:
Method 1: Find Your WordPress Page ID and Post ID without a Plugin
The process of determining your WordPress post or page ID can be completed in a matter of seconds.
Follow these steps to find a page ID:
- Go to Pages in your WordPress dashboard.
- Click on All Pages.
- Click the page that you need to find the ID for.
In this case, I have selected the Privacy Policy page.
- Look at the URL in your web browser’s address bar when the page has opened.
- Find the page ID number displayed within the page URL.
Follow the same procedure as follows to get a post ID from a WordPress URL:
- Go to Posts in your WordPress dashboard.
- Click on All Posts.
- Click the specific post that you need the ID for.
- View the post’s URL in your web browser’s address bar when you are in the post Editor to find the ID number.
Method 2: Find Your WordPress Tag ID and Category ID without a Plugin
Like pages and posts, you can find the tag ID or category ID of a WordPress post or page by following these steps:
- Go to Posts in your WordPress dashboard.
- Click on Categories or Tags.
- Click on the Category that you need the ID for.
- Look for the URL in your web browser’s address when in the specific category editing page bar to find the tags’ ID.
Method 3: Use the Free Reveal IDs Plugin to Find Post and Page IDs
It is possible to use a WordPress plugin to assist you in finding the content IDs on your site if you want all the page IDs and post IDs displayed in the admin menu at once.
As a result, you will be able to save time and effort by not having to switch between editors as is necessary with the first method we discussed.
The Reveal IDs plugin is a popular free WordPress plugin that is easy to install and works immediately.
With this plugin, all post and page IDs, tag category comments, users, and other types of content IDs, will be displayed within the WordPress content menu.
Follow these steps to use the plugin:
- Install and activate the plugin.
- Go to Pages in your WordPress dashboard.
- Click on All Pages.
You will immediately notice that an additional ID column has been added to your Pages menu. This column displays each page’s ID number.
Also, follow these steps to view post IDs:
- Go to Posts in your WordPress dashboard.
- Click on All Posts.
The post IDs will then be displayed in the Posts section of your page.
With this plugin, the tags, categories, and other types of content IDs can be viewed similarly through their respective menus.
Including an ID column for all types of content may be optional.
Follow these steps to hide the ID column for a special menu:
- Go to the menu page.
- Click on Screen Options at the top of the page.
- Uncheck the ID column from the Posts menu.
- Click on the Apply option.
Method 4: Custom Code to Display Post IDs in The Posts Page Columns
In your Posts tab, you will notice that each piece of content contains several pieces of information, including author, tags, categories, etc.
A new column may also be added to this table by editing the functions.php file in your theme. This column will display the ID of each post so that you do not have to search the URL of the post to retrieve this information.
Follow these steps to use a File Transfer Protocol (FTP) client to modify this file:
- Go to your website via FTP.
- Open the WordPress root folder.
- Go to wp-content/themes.
- Look for your theme’s folder inside.
- Open the functions.php file.
You will be able to download the file and open it with your default editor through your FTP client.
- Add this code to the file:
function betterstudio_add_column( $columns ){
$columns['post_id_clmn'] = 'ID'; // $columns['Column ID'] = 'Column Title';
return $columns;
}
add_filter('manage_posts_columns', 'betterstudio_add_column', 5);
function betterstudio_column_content( $column, $id ){
if( $column === 'post_id_clmn')
echo $id;
}
add_action('manage_posts_custom_column', 'betterstudio_column_content', 5, 2);
Your Posts table will now include an additional column due to this code. Suppose you use a plugin that adds additional data to that table. In that case, you might need to modify the position shown in the above code snippet.
As shown above, the new column is added to the fifth position, but you may need to adjust the code as needed.
- Save your changes to functions.php.
With this update, you can view post IDs directly in the table:
Method 5: Find Post IDs Within the WordPress Database
As you may be aware, WordPress maintains a database that stores all information related to your website, including your post, page IDs, and pieces of content IDs.
You can access your site’s database through a custom interface provided by many web hosts.
Follow these steps to find post IDs within the WordPress database:
- Go to your database using phpMyAdmin.
- Open your site’s database.
- Go to the wp_posts table.
To the left of post_author is the ID column, where you should be able to find the ID of each post.
- Copy and paste the post or page IDs you need.
Method 6: Use WordPress Functions to Fetch Post IDs
Depending on your experience, you might not need to look up the ID of a WordPress post if you are a developer. The IDs you require can be obtained using functions with the appropriate parameters.
You can, for example, use the get_the_id() function in order to return the post ID where the code is executed:
get_the_id();
The post ID can also be retrieved by the post title or slug if you feel more adventurous. However, these two methods are slightly less helpful than the previous methods:
$mypost = get_page_by_title( 'Your post title goes here', '', 'post' );
$mypost->ID;
$mypost = get_page_by_path('post-slug', '', 'post');
$mypost->ID;
You can also obtain a post ID from its URL by utilizing the following function:
$mypost_id = url_to_postid( 'https://yourwebsite.com/your-post' );
You can use the following code to determine the post IDs in a WordPress loop:
$id_query = new WP_Query( 'posts_per_page=10' );
while( $id_query-have_posts() ) : $id_query->the_post();
$id_query->post->ID;
endwhile;
A function fetching a WordPress post ID can be beneficial if you are adding custom functionality or developing your plugins.
Suppose you are only interested in finding the IDs for a few specific posts. In that case, you should use one of the alternative approaches discussed above.
Conclusion
This article has provided an understanding of WordPress post and page IDs and different methods for obtaining them in WordPress.
It is our sincere pleasure to have you take the time to read this article. We would appreciate any questions or comments you have in the comment section.
If you want to stay updated with our articles, we recommend following us on Facebook and Twitter.