10 Ways to Get Post ID
I decided to create this super-detailed tutorial so it would never be a problem for you to find out any post ID. When I say “post ID” I also mean pages, custom post types and WooCommerce products and orders.
Just in case you found yourself on this page looking for an opposite information on how to get a post by ID, then all you need to do is to use get_post()
functions, it is really simple, like this:
$post = get_post( 52 );
// let's get a post title by ID
echo $post->post_title;
Now let’s dive into our 14 methods of obtaining post IDs :)
1. In URL when you edit the post
That’s actually the simplest one. If you’re not going to use newfound ID somewhere in the code, you can just find it in URL when you edit this specific post or page.

It is not even required to open post edit page. Just move the mouse over the «Edit» link or over the post title in admin area and then look at the browser status bar (bottom left part of the screen).
2. Add a custom column with post ID
I like this method. If you work with post IDs very often, it should be very helpful for you.

All you have to do is to use the below ready-to-use code. If you do not know where to insert it, I recommend you to read this tutorial.
/**
* Add Post ID Column to WordPress Admin
*
* @author Misha Rudrastyh
* @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
*/
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
$columns[ 'misha_post_id' ] = 'ID';
return $columns;
}
function misha_column_content( $column, $post_id ){
if( 'misha_post_id' === $column ) {
echo $post_id;
}
}
3-5. Get post ID in a loop
As you probably know, there could be different type of loops, for example we can use get_posts()
, WP_Query
or query_posts()
to create a loop. Let’s try to print post IDs for two of these scenarios, because query_posts()
is not a thing that you should use very often.
get_posts()
$articles = get_posts( array( 'posts_per_page' => 50 ) );
foreach( $articles as $article ) {
echo $article->ID;
}
As you can see by default this function returns and array of WP_Post objects, but it can also return an array of post IDs, you you set parameter fields=ids
.
$articles = get_posts( array( 'posts_per_page' => 50, 'fields' => 'ids' ) );
foreach( $articles as $article_id ) {
echo $article_id;
}
WP_Query
$q = new WP_Query( array( 'posts_per_page' => -1 ) );
while( $q->have_posts() ) : $q->the_post();
the_ID();
// or: echo get_the_ID();
// or: echo $q->post->ID;
endwhile;
So, the whole 3 ways how you can get a post ID in a loop created with WP_Query
!
the_ID()
function uses the global$post
variable which is installed from an object available in$q->post
property.get_the_ID()
is similar but it returns the result not prints it.$q->post->ID
is just a post ID obtained directly from an object.
6. Get post ID of a current post (not in a loop exactly)
Let’s imagine a situation when you open single.php
file (or page.php
etc) and you just need to get a post ID in it.
There are three ways how you can do it.
The first way I probably do not recommend, but you still have to know it:
global $post;
echo $post->ID;
The second way is using the_ID()
and echo get_the_ID()
. It is kind of ok, it is widely used, works on base of global $post
variable as well.
But my favourite is this:
echo get_queried_object_id();
It returns the ID of any kind of object which page is displaying right now. So it can be used to get a current category or tag ID as well.
7. Get post ID from permalink (pretty url)
Simple enough.
$post_id = url_to_postid( 'https://rudrastyh.com/wordpress/get-post-id.html' );
The function url_to_postid()
is especially great when pretty permalinks are turned on.
8. Get post ID by title
There is a build-in WordPress function and since 3.0 version it works not only for pages but for any custom post type. Third function parameter is a name of a post type (page
by default).
$my_post = get_page_by_title( 'Hello World', '', 'post' );
echo $my_post->ID;
9. Get post ID by slug
The function is similar to get_page_by_title()
. But if your post has parent (for hierarchical post types only) you have to specify parent slug as well, for example parent-post/hello-world
.
$my_post = get_page_by_path( 'hello-world', '', 'post' );
echo $my_post->ID;
10. By a specific pair of meta key and value
Ok, sometimes you may need it. Of course it is always possible to use meta_query of WP_Query
for that purpose but let me show you a SQL-query solution.
global $wpdb;
$ids = $wpdb->get_col(
$wpdb->prepare(
"
SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s AND meta_value = %s
",
$meta_key,
$meta_value
)
);
// consider that there could be multiple posts with the same meta key and value
// that's why we didn't use $wpdb->get_var();
if( count( $ids ) > 1 ) {
print_r( $ids );
} else {
echo reset( $ids ); // first element of an array
}

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
I want to show content from a specific page in sidebar and use it:
$column_post = get_post('ID post');
How can I done within IT?
Hi Maria,
echo $column_post->post_content
should be goodOutside loop works, but I can not insert the contents of the page in sidebar. How can done within it. Thanks very much for the help, it was useful.
I think you could try to allow PHP in widgets for this purpose (with a plugin or with code).
Thanks for the reply and the time departments. Have useful articles :)
My boss insists a decision without plugins.
Thank you! :)
Your boss’s decision is good. So, you can try 2 ways – find in google simple code for
functions.php
that allows to use PHP inside a text widget or create your own widget.Unfortunately I have no such tutorials but it is very easy to find this information.
Have a great day!
Thanks for your ideas :)
Could I can get the ID of the post that is not yet in the database. I mean, I need the post id inside a form IN THE FRONT END (not admin page), just before to click and submit it.
I think in something like ask to the database: What is the next post id that is going to be this post..
Thank you!
Hi Miguel,
that’s a good question, I think you have to get the next AUTO_INCREMENT value for wp_posts table.
Lo conseguí como decías!
Gracias
Perfecto!
You can even avoid using
foreach
and receiving an array with this:You are right!
Thanks
Thank you.
Thank you
how to add search by postid in list post (wp-admin/edit.php) ?
Hi. Interesting publication. Never knew there would be so many ways to find post ID. Yet how can I do the opposite. How to find out if I have the post ID which exact post it corresponds to?
Thanks
Hi Ivo,
Sorry I do not understand your question.
Hi. I mean is there a way to find which post a certain post-id correlates to?
For instance i have post-id-123 in a CSS style sheet file. How can find the actual post behind this post id?
Thank you.
Hmm, I think we are talking about
$post = get_post( 123 );
.Mmm maybe… but where do I type this command so I get to the actual post?
Thanks!
Sorry I’m that good with php or coding.
In the place where you need to do it 🙃
Like in the style.css file ?
I meant that I’m not that good with php or coding.*
Yes, but in a template file, like
single.php
or something like thatThanks for this article Misha.. You did a great job. Hats off to you. I have one query. I tried to call the post and page id through url to post id method but I didn’t get the ID…. Can you please help me out. Nothing happens while declaring that php function.