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

To find a WordPress post or page ID without code, open it in the editor and read the number after post= in your browser’s address bar. In PHP, use get_the_ID() inside the loop, get_queried_object_id() anywhere else on the current page, and url_to_postid() or get_page_by_path() to look one up from a URL or slug.

Every post, page, attachment and custom post type entry gets a unique numeric ID, and it stays the same when you change the title or slug. That is why plugins ask for IDs and why code snippets rely on them. We ran every snippet below on WordPress 7.1.4, including read-only checks against SmartWP itself, so the outputs in this guide are real.

Key Takeaways
  • In the dashboard, the ID is the number in the editor URL: post.php?post=1824&action=edit. Pages use the same format.
  • Use get_the_ID() inside the loop and get_queried_object_id() outside it. On the blog page, get_the_ID() returns the first post’s ID, not the page’s.
  • get_page_by_path() searches pages by default, so pass 'post' as the third argument for blog posts or it returns null.
  • get_page_by_title() has been deprecated since WordPress 6.2. Use WP_Query with the title argument instead.
  • Once you have an ID, get_post( $id ) returns the full post object, or null if no post has that ID.

Which method should you use?

Where you areUseReturns
WordPress dashboardThe editor URL, or the Reveal IDs pluginThe number after post=
Inside the loopget_the_ID()int, or false outside a post
Current page, outside the loopget_queried_object_id()int (0 if nothing is queried)
You have a slugget_page_by_path()WP_Post or null
You have a URLurl_to_postid()int (0 if no match)
You have a titleWP_Query with titleArray of IDs
Block editor JavaScriptgetCurrentPostId()Number
Command linewp post listID column or value
Every method tested on WordPress 7.1.4.

1. Find a post ID in the WordPress dashboard

Go to Posts > All Posts (or Pages > All Pages) and hover over the post. Its admin row shows an Edit link; click it, or just hover and check the link in your browser’s status bar.

WordPress 7.1 Posts screen with the row actions showing under Hello world: Edit, Quick Edit, Trash and View
Hover over a post to show its Edit link

The editor opens at a URL like this, and the number after post= is the ID:

https://example.com/wp-admin/post.php?post=1824&action=edit
WordPress post ID 1824 shown in the browser address bar of the post editor
The ID of this article, 1824, in the editor URL

Pages, media and custom post types use the same post.php?post= format, so this works for any of them.


2. Show an ID column with the Reveal IDs plugin

If you look up IDs often, the free Reveal IDs plugin adds sortable ID columns back to most admin list screens, including Posts and Pages. It has more than 40,000 active installs and is tested with WordPress 7.1.

WordPress 7.1 Posts screen with an ID column added by the Reveal IDs plugin, showing ID 1 for Hello world
The ID column added by Reveal IDs

3. Get the current post ID with get_the_ID()

get_the_ID() returns the ID of the current post in the loop, which covers single.php, page.php and block template parts. Outside a post context it returns false.

<?php
$post_id = get_the_ID();

if ( $post_id ) {
    echo 'The current post ID is: ' . $post_id;
}

the_ID() does the same thing but echoes the ID directly.


4. Get the current page ID anywhere with get_queried_object_id()

In a header, sidebar, plugin hook or the blog page, the loop may not point at the page you think. get_queried_object_id() returns the ID of whatever the main query is showing, whether that is a post, a page or a term.

<?php
$page_id = get_queried_object_id();

The difference shows up on the blog page. On SmartWP, the Blog page has ID 8. On that page, get_queried_object_id() returned 8, while get_the_ID() inside the loop returned 4517, the ID of the newest post in the list. If your code needs the page itself, use get_queried_object_id().


5. Get a post ID by slug

get_page_by_path() finds a post from its slug, but it searches pages unless you say otherwise. On SmartWP, get_page_by_path( 'wordpress-get-post-id' ) returned null, because this article is a post. Passing the post type returned 1824:

<?php
$post = get_page_by_path( 'my-post-slug', OBJECT, 'post' );

if ( $post ) {
    echo 'The post ID is: ' . $post->ID;
}

For a child page, pass the full path, such as 'parent-page/child-page'. If you need the slug first, see how to get the current page slug in WordPress.


6. Get a post ID from a URL

url_to_postid() turns a URL on your site into a post ID. It returned 1824 for this article’s URL and 0 for a URL that doesn’t exist, so check for zero.

<?php
$post_id = url_to_postid( 'https://example.com/my-post/' );

if ( $post_id ) {
    echo 'The post ID is: ' . $post_id;
}

To use it on the page you are viewing, combine it with the current URL, though get_queried_object_id() is simpler for that.


7. Get a post ID by title

get_page_by_title() has been deprecated since WordPress 6.2, and the reference recommends WP_Query instead. Its title argument matches the exact title:

<?php
$query = new WP_Query(
    array(
        'post_type'      => 'post',
        'title'          => 'My Post Title',
        'posts_per_page' => 1,
        'fields'         => 'ids',
        'no_found_rows'  => true,
    )
);

$post_id = $query->posts ? $query->posts[0] : 0;

Titles are not unique, so prefer the slug or URL when you can.


8. Get the post ID in the block editor (JavaScript)

Inside the block editor, the core/editor data store has a getCurrentPostId selector. You can run it in your browser’s console while editing a post, or use it in your own block code:

wp.data.select( 'core/editor' ).getCurrentPostId();

9. Get a post ID with WP-CLI

If you have WP-CLI access, wp post list looks up IDs by slug or lists them in bulk. Running the first command on SmartWP returned 1824:

wp post list --name=wordpress-get-post-id --fields=ID,post_title

# Just the IDs of your pages
wp post list --post_type=page --field=ID

Get a post by its ID

Once you have an ID, get_post() returns the full WP_Post object, or null if nothing has that ID (we checked with an ID that doesn’t exist):

<?php
$post = get_post( 1824 );

if ( $post ) {
    echo $post->post_title . ' (' . $post->post_type . ')';
}

From there, the most common next steps are printing just the title by passing the same ID to get_the_title(), reading custom fields with get_post_meta(), or filtering posts with a meta query.

Frequently asked questions

How do I find a page ID in WordPress?

The same way as a post ID. Go to Pages > All Pages, open the page in the editor, and read the number after post= in the address bar. In code, get_queried_object_id() returns the current page’s ID, and get_page_by_path( ‘page-slug’ ) finds a page by its slug.

Why does get_the_ID() return the wrong ID?

Usually because you are outside the loop or on the blog page, where the loop points at the posts being listed rather than the page itself. Use get_queried_object_id() to get the ID of the page being viewed.

How do I get the ID of the front page or the blog page?

If your site uses a static front page, get_option( ‘page_on_front’ ) returns the front page ID and get_option( ‘page_for_posts’ ) returns the blog page ID. On SmartWP they return 10 and 8.

Does a post ID change if I change the title or slug?

No. The ID is the post’s primary key in the database, so it stays the same when you edit the title, slug or content. It only changes if the post is deleted and recreated, or imported into another site, where it can get a new ID.

How do I get a WooCommerce product ID?

Products are a post type, so the methods above work for them too. For WooCommerce-specific options, such as getting the ID from a product object or a cart item, see our guide to getting a WooCommerce product ID.

Once you have the ID, you can pass it to plugin settings that target specific content, read its data with get_post(), or update it with the functions in our guide to inserting and updating posts programmatically.

Picture of Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 15 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.