WordPress Get Current Page Id and Post Id

WordPress get current page id is a common question asked by a WordPress developer especially if you’re trying to exclude certain pages by Page ID. While you are developing WordPress themes or plugins you are not always working inside the WordPress Loop.

In this tutorial, we will show you how to get WordPress page id and WordPress post id using different methods.

1. WordPress Page Id from Post Edit Page URL

You can easily spot on the WordPress page id if you move to WordPress post edit page. You will find the id of any post or page in the URL of your WordPress post edit page. Just like below.

WordPress Page Id from Post Edit Page URL

WordPress Page Id from Post Edit Page URL

We personally use this method as this doesn’t require any action apart from opening the desired Post or Page.

You can skip the opening of Post edit page and just hover over the desired post in your posts section of the WordPress admin.

While hovering over the Post title, look at the browser status bar and you can easily find the post or page id. Look for post= and the number to the right of it is your Page ID. In the image below, the ID is 675.

WordPress Page Id from Status Bar

WordPress Page Id from Status Bar

2. WordPress Get Current Page Id from Database

If you are familiar with MySQL databases and have access to phpMyAdmin, you can get the WordPress post or page id from the wp-posts table just like below.

WordPress Get Current Page Id from Database

WordPress Get Current Page Id from Database

Please note: All the following examples require some basic PHP knowledge.

3. Get WordPress Page or Post Id by a Post Title

There is a built-in function in WordPress to get a post object by its title. This function not only works for the post but also your custom post types if you have any.

You can get a post object within your WordPress theme or plugin by using below code snippet.

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

In the first parameter of this function you will pass the Post or Page title and in the third parameter, you can specify post type in our case its post.

4. Get WordPress Page or Post Id by a Slug

Same as above you can get the post object by its slug. You can use the below code snippet to get the post object and then extract the Id of the page.

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

The only difference between get_page_by_path() and get_page_by_title() is the type of data you supply. If you have Page hierarchy in your pages structure then you have to pass the parent page slug as well like this parent-page-slug/hello-world.

5. WordPress Get Post Id Outside the WordPress Loop

WordPress provides a very powerful post object called Global Post Object. It contains a lot of data of the current post. You can easily grab the post or page id from it.

echo $post->ID;

Warning: When you are trying to use global post object within a function of your WordPress theme or plugin you have to declare the global post firstly. Just add global $post; line at the beginning of the function.

global $post;
echo $post->ID;

6. WordPress Get Post Id Inside the WordPress Loop

When you are working within the WordPress Loop, you can grab the WordPress page id very easily. WordPress Loop provide get_the_id() and the_ID(); to get the id.

echo get_the_id();
the_ID();

The only difference between above two is get_the_id() returns the page or post id while the_ID() prints the id.

7. WordPress Post Id Column in Posts Listing Page

If you are working heavily with the post or page ids then it would be a lot better to add WordPress page or post id into the posts listing page table. Here we are sharing below code snippet which will add an extra column in the posts table called ID and will show the post id in that columns. Just like below.

WordPress Post Id Column in Posts Listing Page

WordPress Post Id Column in Posts Listing Page

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


function webwhistles_add_column( $columns ){
$columns['webwhistles_post_id_col'] = 'ID';
return $columns;
}
add_filter('manage_posts_columns', 'webwhistles_add_column', 3);
//add_filter('manage_pages_columns', 'webwhistles_add_column', 3); // for Pages

function webwhistles_column_content( $column, $id ){
if( $column === ‘webwhistles_post_id_col’)
echo $id;
}
add_action(‘manage_posts_custom_column’, ‘webwhistles_column_content’, 3, 2);
//add_action(‘manage_pages_custom_column’, ‘webwhistles_column_content’, 3, 2); // for Pages

Warning: I always recommend to backup your functions.php file before making any changes. Just in case if you broke anything.

Over to you

There you have it, 7 easiest ways to get WordPress page id or post id. This will help you a lot when you are working with page ids. If you have any suggestion or question, please leave a comment below.

Leave a Reply