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

The post ID in WordPress is a unique number assigned to every post, page, and custom post type. Whether you need it for a plugin setting or a custom code snippet, here are 6 ways to get a WordPress post ID.

Since titles and content can change, WordPress relies on these numeric IDs as permanent identifiers. Many plugins ask for a post ID to exclude content from a feature or target specific pages. If you’re a developer, you’ll also need post IDs when working with functions like get_post() or meta queries.


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 its post ID in the URL of your browser.

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

View all posts in WordPress dashboard
View all posts in WordPress

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

Click edit on a WordPress post to view its ID

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

WordPress post ID shown 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)

The get_the_ID() function returns the ID of the current post. It works inside the WordPress loop and in template files like single.php or page.php.

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

You can also use the_ID() to directly echo the current post’s ID without storing it in a variable.


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. If you need help finding a slug first, see how to get the current page slug in WordPress.

<?php
$post_data = get_page_by_path('my-post-slug');
$post_id = $post_data->ID;
if(!empty($post_id)){
echo 'The post ID is: '.$post_id;
}

5. Get Post ID by Title (using PHP)

Note: The get_page_by_title() function has been deprecated since WordPress 6.2. The code below still works, but WordPress now recommends using WP_Query instead. Make sure to specify which post type you’re trying to retrieve as seen below (post/page).

<?php
$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)

The url_to_postid() function converts a URL into a post ID. If you need to get the current URL in WordPress first, you can combine both functions.

<?php
echo 'Returning a specific post ID from a url: '.url_to_postid( 'https://smartwp.com/my-page-url' );

Those are 6 ways to find a WordPress post ID, from the dashboard to PHP functions. Once you have the ID, you can use it with functions like wp_insert_post() or pass it to plugin settings that target specific posts and pages.

Picture of 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.