14 Ways to Get Post ID in WordPress

In this tutorial I will show you how to get post ID by: post title, post slug, post URL, meta key, pair of meta key and meta value, category/tag ID etc.

  /   24

Please keep in mind, that all method below works for WordPress pages and custom post types too.

If you are looking for information how to do the opposite – get post by ID, all you need is to use get_post() function, example:

$post = get_post( 52 );
// let's get a post title by ID
echo $post->post_title;

Or you can get post by ID with WordPress WP_Query too:

$query = new WP_Query( array( 'p' => 52 ) );
// let's get post type by ID
echo $query->post->post_type;
// Get post status by post ID
echo $query->post->post_status;

But now let’s continue to the main part of the tutorial 👍

1. In URL on the post edit page #

I usually use this simple method for myself. You will find ID of the post in URL when editing it.

You can find ID of a specific post in its URL when you edit it.

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).

Just hove the post Edit link or the post Title and look at the bottom of you browser window. You will find post ID there.

2. In URL of the Post Without Custom Permalink Structure #

If your permalinks settings in Settings > Permalinks are set to Plain, then the p parameter in URL is the ID of the post.

Post ID as a part of post URLs if permalinks settings are set to plain.

Well, it was really for beginners.

3. Add the Post ID column to the WordPress Posts Table #

I like this method. If you work with post IDs very often, it should be very helpful for you (screenshot with example is under the code).

All you need to do is just to insert this code to your functions.php file of your active WordPress theme.

 * Just adds a column without content
 * @param array $columns Array of all the current columns IDs and titles
function misha_add_column( $columns ){
	$columns['misha_post_id_clmn'] = 'ID'; // $columns['Column ID'] = 'Column Title';
	return $columns;
add_filter('manage_posts_columns', 'misha_add_column', 5);
//add_filter('manage_pages_columns', 'misha_add_column', 5); // for Pages
 * Fills the column content
 * @param string $column ID of the column
 * @param integer $id Post ID
function misha_column_content( $column, $id ){
	if( $column === 'misha_post_id_clmn')
		echo $id;
add_action('manage_posts_custom_column', 'misha_column_content', 5, 2);
//add_action('manage_pages_custom_column', 'misha_column_content', 5, 2); // for Pages
Example of adding the Post ID column to posts table in WordPress admin area.
You can add this column for «Pages» or another Custom Post Types as well.

It is also possible to change column width, to place if before the post title or to make it sortable. If you would like to implement something of that but don’t know how — please ask me in comments and I will give you ready-to-use code.

4. Post ID in WordPress Database #

Are you familiar with MySQL databases? If yes, you can find post IDs there in wp_posts table (or {any_database_prefix}posts table).

ID column in WordPress database.

5. From the Global $post object #

Please note: All the following examples require some basic PHP knowledge. But if you have not enough knowledge — do not be afraid of trying.

Global object $post contains a lot of data of the current post. It is very easy to get ID from the object:

echo $post->ID;

Sometimes, when you are using it inside a function (or in any case when it prints nothing :) just add global $post; line of code at the beginning.

global $post;
echo $post->ID;

6. Using get_the_id() and the_id() functions #

The difference between this two functions is that get_the_id() returns ID of the current post, and the_id() prints it.

echo get_the_id();

7. Get Post ID by Title #

This is a build-in WordPress function and since 3.0.0 version it works not only for pages but for any custom post type. Third function parameter is the name of a post type (page by default).

$mypost = get_page_by_title( 'Hello World', '', 'post' );
echo $mypost->ID;

8. 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.

$mypost = get_page_by_path('hello-world', '', 'post');
echo $mypost->ID;

9. Get Post ID by URL #

Very simple function.

$mypost_id = url_to_postid( 'https://rudrastyh.com/wordpress/get-post-id.html' );

10. Get Post ID shown on the front page #

Well, this example is for page post type only. You can get in the code IDs of both pages that you see on the screenshot below:

Reading settings, what page to display on the fron page and what page should be a posts page.
$frontpage_id = get_option( 'page_on_front' );
$blogpage_id = get_option( 'page_for_posts' );

11. Easy Way to Get Post ID in a WP_Query loop #

When you use WP_Query or another additional loops it is always much better and faster to get the $post object properties instead of using get_the_id() or the_id() functions.

$your_custom_query = new WP_Query( 'posts_per_page=10' );
while( $your_custom_query-have_posts() ) : $your_custom_query->the_post();
	echo $your_custom_query->post->ID; // print post ID

12. Get Post ID by meta key #

In fact there are two ways to do it — using WP_Query or MySQL query. Here I will introduce you a function that returns a post ID or an array of post IDs that match the given meta key.

 * Returns matched post IDs for a meta key from database
 * @param string $meta_key
 * @return array Array of post IDs - if more than one post matched has this meta key
 * @return int Post ID - if just one post found
function misha_post_id_by_meta_key( $meta_key ){
	global $wpdb;
	$ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key ) );
	if( count( $ids ) > 1 ) 
		return $ids; // return array
		return $ids[0]; // return int

13. Get Post ID both by meta key and value #

It is similar to the previous function, but here you can also specify a meta value.

 * Returns matched post IDs for a pair of meta key and meta value from database
 * @param string $meta_key
 * @param mixed $meta_value
 * @return array|int Post ID(s)
function misha_post_id_by_meta_key_and_value( $meta_key, $meta_value ){
	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 ) );
	if( count( $ids ) > 1 ) 
		return $ids; // return array
		return $ids[0]; // return int

14. Get Post ID by Term ID (Category ID, Tag ID) #

 * Returns post IDs in a term with a given ID
 * @param int $term_id
 * @return array|int Post ID(s)
function misha_post_id_by_term_id( $term_id ){
	global $wpdb;
	$ids = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term_id ) );
	if( count( $ids ) > 1 ) 
		return $ids; // return array
		return $ids[0]; // return int

In fact, using $wpdb you can get IDs of posts by any parameter — category name, publish date, number of comments, even by comment author email (!) etc. If you need more examples, ask me in comments.

Misha Rudrastyh
About the author Misha Rudrastyh

Passionate about WordPress and snowboarding, creating websites for over 10 years! Let's work together — just contact me.

If you are a developer too, subscribe to my facebook page.

Comments 24

  • I want to show content from a specific page in sidebar and use it: $column_post = get_post(‘ID post’);
    $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($column_post->ID), ‘twentyseventeen-featured-image’);
    How can I done within IT?

    • Hi Maria,


      echo $column_post->post_content, or if you want to apply filters, then:

      $content_filter_applied = apply_filters( 'the_content', $column_post->post_content );
      echo str_replace( ']]>', ']]>', $content_filter_applied );

      P.S. In your example above do not forget echo $thumbnail[0].

      • <?php if ( is_active_sidebar( 'sidebar-1' )  ) : ?>
        	<aside id="secondary" class="sidebar widget-area" role="complementary">
        		<?php dynamic_sidebar( 'sidebar-1' ); ?>
        	</aside><!-- .sidebar .widget-area -->
        <?php endif; ?>
        <?php $column_post = get_post('149'); 
                           echo $column_post->post_content ?>

        Outside loop work, 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!

    • MishaAuthor

      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!

        global $wpdb;
        $result = $wpdb->get_results ( "SHOW TABLE STATUS LIKE 'your_table'" );
        foreach ( $result as $page ){
        	$get_id = $page->Auto_increment;


  • 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?


Comments are closed.
If you need my personal help, please contact me. Contact me