How to Get WordPress Post and Page IDs (Code Snippets)

The post ID in WordPress can be used for tons of reasons. Whether it’s for a plugin or custom code here are 6 ways to get WordPress post ID.

Post and page IDs are used in WordPress to identify the piece of content on your site. Since titles, content, and other data can change WordPress uses these IDs as unique identifiers for your content. Many plugins will allow you to use a post ID in their functionality, for example excluding a post from a function. Additionally if you’re a WordPress developer getting the ID of a post or page can be used for various functions.

Here are 6 ways to get a post ID in WordPress:

1. Find Post ID in WordPress Dashboard

The easiest way to get the ID of a post in WordPress is to login to your site and edit a post. While editing a post in WordPress you can see it’s post ID in the URL of your browser.

This can be done by heading to Posts>all posts.

View all posts in WordPress

Here you can click “edit” on the post you want to see the post ID of.

After selecting a post you’ll be able to see the post ID in the browser URL bar.

This is great for one-off use cases like getting a post or page ID for use in a plugin’s function.

2. View Post IDs using WordPress Plugin

If you just want to see your post IDs in your WordPress dashboard as a column you can use the plugin Reveal IDs.

After enabling the plugin you’ll see an ID column in your WordPress dashboard for all post types.

Post ID column in the dashboard

3. Get Current Post ID (using PHP)

Want to get the current post ID of the current post? Using the get_the_ID() function will easily get post ID. This works in template files or inside any loop.

echo 'The current post ID is: '.get_the_ID();

You can also use the the_ID() function to install echo the current post’s ID.

4. Get Post ID by Slug (using PHP)

The get_page_by_path() function allows you to get post data in WordPress using the post slug. So in this example we will be getting the post ID from its slug.

$post_data = get_page_by_path('my-post-slug');
$post_id = $post_data->ID;
echo 'The post ID is: '.$post_id;

5. Get Post ID by Title (using PHP)

Using the get_page_by_title() function allows us to get a post’s ID by its title. Just make sure to specify which post type you’re trying to retrieve as seen below (post/page).

$post_details = get_page_by_title( 'My Post Title', '', 'post' );
echo $post_details->ID;
$page_details = get_page_by_title( 'My Page Title', '', 'page' );
echo $page_details->ID;

6. Get Post ID by Full URL (using PHP)

Using url_to_postid() allows us to get a post’s ID by using its URL.

echo 'Returning a specific post ID from a url: '.url_to_postid( '' );

I hope this guide to finding the post id in WordPress was useful. If you have any questions about WordPress development let us know in the comments below.

Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 10 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way. Found this article helpful? Buy Me A Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Tips Monthly
Get the latest from SmartWP to your inbox.