Get Post ID WordPress

When you create a post or page in WordPress, the platform automatically assigns it an ID number or unique identifier. There are many scenarios in which you might want to get this WordPress post ID; for instance, if you need to exclude particular articles from a content grid or want to code a custom query.

Unfortunately, WordPress does not share the post ID openly, so you will need to follow a few steps to locate it. 😎

In this article, we will explain 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 to display 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 recognize specific content. For example, a plugin might need to know these details to include or exclude certain articles from a particular function. If you are a developer, you might also use these IDs to add custom code to your WordPress website or build shortcodes (most of them use post and page IDs as parameters, so it’s important to know them).

For example, if you want to target a specific page to change how your site looks and functions, you might write something like this:

if(is_single(POST_ID))
{
	//...
}

In this case, POST_ID is the unique identifier for the content you want to locate and use. Now, you would think finding this information would be easy, considering millions of people use (and adapt!) their WordPress theme on a daily basis. The truth is, finding post IDs is indeed relatively straightforward – but only if you know where to look!

Best ways to get WordPress post IDs

There are five main approaches you can use to view post IDs. Let’s go through each of them in a little more detail and see which is best for each case.

Option 1: Use the WordPress dashboard or URL

Using your WordPress dahboard is the easiest and quickest way to identify WordPress post IDs. To do this, just go to your menu options and click on Posts. This will show a list of posts on the right-hand side of your page.

To get their IDs, all you need to do is hover your mouse over the title. You will see the number at the bottom of your browser window:

Finding a post ID from the WordPress dashboard

If you have trouble seeing 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!

Locating a post ID from the browser address bar

One important thing to keep in mind is that the post address bar will show a different format when you edit an article compared to what your users see when they visit your site. This is because WordPress allows you to pick your own permalink structure, which means your address might not show IDs at all to your visitors. In fact, if you set up your permalinks correctly, it should not and will not publicly show the ID at all.

Option 2: Show your post IDs in the posts tab

This method is a little more advanced, but if you use IDs a lot, it’s worth considering. You can display post IDs directly on your Posts tab; all you need to do is edit the functions.php file to add a column title containing it.

The way you do this is by locating this file (which is usually under your wp-content/themes 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. Because the example above uses the “4” column, the ID will be shown in the 4th position starting from your left. You can, of course, adjust this code as you prefer.

post IDs in wp-admin

Option 3: Get your IDs using a PHP function

For developers, another easy way to fetch post IDs is to use actual PHP functions that have been built into WordPress from the start. These functions let you reference post IDs directly and do so in different places throughout your own custom code.

The function is actually quite straightforward:

get_the_id();

Of course, you can also find post ID numbers using the post slugs 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( 'https://YOURWORDPRESSSITE.com/unique-post' );

Lastly, you can fetch IDs 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;

Although finding IDs this way can come in handy when you’re writing custom code, if all you need to do is get the number for just a few pages or posts, the other methods will work better.

Option 4: Use a plugin to display post IDs

If you don’t want to edit your functions.php file, something you can do instead is install a plugin that can display not just your post IDs but also the numbers for your pages, tags, categories, media files, and custom taxonomies and post types.

One good option is ShowIDs, a lightweight free plugin that doesn’t need any configuration. You just activate it, and it will automatically show your IDs on a column present on all admin pages.

Option 5: Locate your post data in the WordPress database

If you’re already familiar with the WordPress database, you might suspect that the post ID is also stored there. In fact, the database will contain everything related to your content, such as categories, tags, authors, dates, comments, and more.

To locate your post IDs using phpMyAdmin, all you need to do is:

  • Go to your hosting control panel (or cPanel – most hosts use it).
  • Click on Databases → phpMyAdmin.
  • Choose a database on the left side (most sites will only have one) and click Enter phpMyAdmin. Usually, the database that WordPress uses starts with wp_.
  • Find and click on wp_posts. You will see the post ID column in the fourth position.

Make sure you don’t change anything on your database unless you are familiar with the interface and understand the information that is stored here.

The post ID column within phpMyAdmin

Conclusion 🧐

As we have seen, finding your WordPress post and page IDs is easy if you know where to look.

Our recommended technique, if you need to locate a few of these unique identifiers, is to simply use the dashboard. This will be the fastest approach to finding IDs and the most straightforward solution if you want to avoid touching any code or installing any plugins on your site.

However, if you need to use IDs more frequently, you should consider adding an option that can display them in a column. You can do this by editing your functions.php file or by using a plugin (there are free ones that can do it for you). If you are a more advanced user, though, you can take advantage of your database to showcase this and more post information.

Yay! 🎉 You made it to the end of the article!

0 Comments
Inline Feedbacks
View all comments

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!