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.

how to find out post ID by URL of its edit 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.

post ID column in WordPress admin
You can add this column for “Pages” or other Custom Post Types as well.

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!

  1. the_ID() function uses the global $post variable which is installed from an object available in $q->post property.
  2. get_the_ID() is similar but it returns the result not prints it.
  3. $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

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

Follow me on Twitter