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.

September 6, 2016 / #Basics, #Compilation

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();
the_id();

7. Get Post ID by a Post 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 a Post 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 a 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
endwhile;

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

Only the best of WordPress

once a week, no spam

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email