When you create a post or page in WordPress, the platform automatically assigns it an ID number or unique identifier. There are several scenarios in which you would want to get this WordPress post ID; For example, if you need to exclude particular articles from a content grid or you want to code a custom query.
Unfortunately, WordPress doesn’t share the post ID openly, so you’ll need to follow a few steps to find it.
In this article, we will explain to you everything you need to know about how to find a post id in WordPress – From a simple solution that only requires you to use your mouse, to more advanced techniques for displaying IDs in columns and new dashboards. So, let’s jump right in!
What is Post ID and why do you need it?
As we mentioned, post and page IDs are unique identifiers that WordPress uses to identify specific content. For example, a plugin may need to know these details in order to include or exclude certain articles from a particular function. If you are a developer, you can also use these IDs to add custom code or create shortcodes in your WordPress website (most of them use to post and page IDs as parameters, so it is important to know them ).
For example, if you want to target a specific page to change the way your site looks and functions, you can write something like this:
if(is_single(POST_ID))
{
//...
}
in this matter, post_id is the unique identifier for the content you want to find and use. Now, you would think that this information would be easy to find, considering that millions of people use (and customize!) their WordPress themes on a daily basis. The truth is that finding a post ID is actually relatively simple – but only if you know where to look!
Best way to get a WordPress post id
There are five main ways you can use to look up post IDs. Let’s look at each of them in a little more detail and see which one is best for each case.
Option 1: Use WordPress Dashboard or URL
Using your WordPress dashboard is the easiest and fastest way to identify the WordPress post ID. To do so, simply go to your menu options and click posts, This will show a list of posts on the right side of your page.
To get their ID, all you need to do is hover your mouse over the title. You’ll see the number at the bottom of your browser window:
If you’re having trouble viewing the ID this way, you can also click on the post to open it. This will take you to the editor. The URL will also show the ID (62805 in the example below) in the navigation bar at the top of your web browser!
One important thing to keep in mind is that when you edit an article the post address bar will show a different format than what your users see when they visit your site. This is because WordPress allows you to choose your own permalink structure, which means your address may not show your visitors an ID at all. In fact, if you set up your permalinks correctly, it should be No and will No Show ID publicly at all.
Option 2: Show your post ID in the Posts tab
This method is a bit more advanced, but it’s worth considering if you use IDs a lot. You can display the Post ID directly on your Posts tab; all you have to do is edit functions.php file to add column headings to it.
The way you do this is by locating this file (which is usually in your wp-content/theme folder) and adding the following code to it:
function add_column( $columns ){
$columns['post_id_clmn'] = 'ID';
return $columns;
}
add_filter('manage_posts_columns', 'add_column', 4);
function column_content( $column, $id ){
if( $column === 'post_id_clmn')
echo $id;
}
add_action('manage_posts_custom_column', 'column_content', 4, 2);
What this snippet will do is add a new ID column. Since the “4” column is used in the example above, the ID will be shown in the fourth position starting from your left. You can of course adjust this code to your liking.
Option 3: Get Your ID Using PHP Function
For developers, another easy way to fetch post IDs is to use the actual PHP functions that are built into WordPress from the start. These functions allow you to reference the post ID directly and do so in various places in your custom code.
The function is actually quite straightforward:
get_the_id();
Of course, you can also find the post ID number using the post slug or post title, or even the post’s URL:
$mypost = get_page_by_path('post-slug', '', 'post');
$mypost->ID;
$mypost = get_page_by_title( 'The title of your post', '', 'post' );
$mypost->ID;
$mypost_id = url_to_postid( ' );
Finally, you can get the ID in a WordPress loop:
$id_query = new WP_Query( 'posts_per_page=6 );
while( $id_query-have_posts() ) : $id_query->the_post();
$id_query->post->ID;
endwhile;
While finding IDs this way can be useful when you’re writing custom code, if you only need to get the numbers for certain pages or posts, other methods will work better.
Option 4: Use a Plugin to Display Post ID
If you don’t want to edit your functions.php file, instead what you can do is install a plugin that can display not only your post IDs but also the number of your pages, tags, categories, media files, and custom taxonomies and post types.
A good alternative is ShowIDs, a lightweight free plugin that doesn’t require any configuration. You just activate it, and it will automatically show your ID on the column you want on all admin pages.
Option 5: Locate Your Post Data in the WordPress Database
If you are already familiar with the WordPress database, you might suspect that the post ID is stored there as well. In fact, the database will contain everything related to your content, such as categories, tags, authors, dates, comments, and more.
To find out your Post ID using phpMyAdmin, all you need to do is:
- Go to your hosting control panel (or cPanel – most hosts use it).
- click on Database → phpMyAdmin,
- Select a database on the left (most sites will only have one) and click Enter PhpMyAdmin, Usually, the database that WordPress uses starts with
wp_, - find and click, you will see the post
IDPillar is in fourth place.
Make sure you don’t change anything in your database unless you are familiar with the interface and understand the information stored here.
Conclusion
As we’ve seen, finding your WordPress post and page IDs is easy if you know where to look.
Our recommended technique, if you need to locate some of these unique identifiers, is to simply use the dashboard. If you want to avoid touching any code or installing any plugins on your site this will be the fastest way and most direct solution to find the id.
However, if you need to use the IDs more often, you should consider adding an option that can display them in a single column. you can do it by editing the functions.php file or by using a plugin (there are free ones that can do this for you). If you’re a more advanced user, though, you can leverage your database to make it show you even more post information.
