How to Find WordPress Post ID and Page ID

Every WordPress post or page has a unique number, called a post or page ID. It functions as an identifier for each piece of website content. Since WordPress doesn’t have a built-in feature to show post IDs, finding them can be tricky.

WordPress developers mainly use post and page IDs for website customization. If you’re a beginner, you may wonder why you should use WordPress post IDs and how to use them.

This article is an all-in-one guide for WordPress post and page IDs. We’ll explain the basics and why they are important.

Download Ultimate WordPress Cheat Sheet

What Is a WordPress Post ID and Page ID?

A post ID is a unique number assigned to each post on a WordPress website. Each page also contains an identifying number, called a page ID.

WordPress uses IDs to keep track of every piece of content within the WordPress database. Other than posts and pages, there are IDs for media attachments, categories, and tags. These unique IDs will remain unchanged even if you rename or edit your posts and pages.

Why Do You Need to Find Post IDs and Page IDs?

You may need a post or page ID in several instances. For example, building a custom shortcode requires a unique ID number as the parameter. When adding widgets to a WordPress theme, you may also need to identify the post ID.

Here are some other cases where you can use a page ID or post ID:

  • Creating custom post or page orders without interrupting other WordPress site content
  • Including or excluding specific posts from a custom list
  • Specifying a post or page in a custom template file

How to Find WordPress Post IDs and Page IDs Manually: Easy Methods

There are three simple ways to find WordPress page and post IDs:

  • Via the site URL within the WordPress admin area
  • Via the URL with the plain permalink
  • Using the WordPress text editor

Let’s go over the steps for each of these methods.

1. Via the URL Within the WordPress Dashboard

Here’s how to locate post, page, and category IDs within URLs from the WordPress admin dashboard.

Post ID

  1. From the WordPress dashboard, navigate to Posts -> All Posts.
  2. Hover over one of your post titles. For example, we’ll hover over a post entitled “What Is HTML? Hypertext Markup Language Basics Explained”.
WordPress Posts screen, highlighting a post entitled What Is HTML? Hypertext Markup Language Basics Explained
  1. At the bottom left corner, a URL will appear. The post ID is the number between the “post=” and “&” parameters.

Page ID

  1. Go to Pages -> All Pages.
  2. Hover over a WordPress page title. For example, we’ll choose the Home page.
WordPress Page screen, highlighting a page entitled "Home".
  1. At the bottom left corner, a URL will appear. The WordPress page ID is the number between the “post=” and “&” parameters.

Category ID

  1. Navigate to Posts -> Categories.
  2. Hover over a category.
WordPress Categories screen, highlighting a category entitled Category
  1. At the bottom left corner, a URL will appear. The category ID is the number between the “category&tag_ID=” and “&” parameters.

If you set the permalink structure to the default or plain permalink, the “post=” parameter in the URL will change to “p=”. Meanwhile, the “category&tag_ID=” parameter changes to “cat=” for the category ID and “tag=” for the tag ID.

Here’s how to find a post ID in WordPress with the URL set to the plain permalink structure.

  1. From the WordPress admin dashboard, navigate to Settings -> Permalinks.
  2. If you haven’t set the permalink to plain, change it to Plain and click Save Changes.
WordPress Permalink Settings screen, highlighting the option Plain.
  1. Go to Posts -> All Posts. Choose a post and click View.
WordPress Posts screen, highlighting the View button.
  1. You will be taken to the website preview page. The post ID is the number following the “p=” parameter in the URL.
Web browser's address bar, highlighting a post ID.

3. Using the WordPress Text Editor

You can also find WordPress post IDs using the WordPress text editor.

  1. Navigate to Posts -> All Posts. To identify a WordPress page ID, navigate to Pages -> All Pages instead.
  2. Choose a post or page and click Edit to open the WordPress text editor.
  3. For example, we used a post entitled “How to Make a Website From Scratch: A Beginner’s Guide”.
WordPress Posts screen, highlighting the Edit button.
  1. Locate the post ID number in the address bar. It’s in the URL, located between the “post=” and “&” parameters.

How to Find a WordPress Post ID or Page ID With a Plugin

You can use a WordPress plugin to find post and page IDs. Since there are many WordPress plugins to choose from, we recommend Reveal IDs.

The Reveals IDs official website, inside the "WordPress" page.

Reveal IDs Stats:

  • Downloads: 60,000+
  • Rating: 5/5
  • Notable features: sortable columns, lightweight, and multilingual support
  • Best suited for: advanced WordPress users or developers
  • Price: free

Reveal IDs is a WordPress plugin that lets you see WordPress IDs for posts, pages, categories, and tags within the admin page. This free plugin is lightweight, multilingual, and regularly updated.

After installing the WordPress plugin, navigate to Posts -> All Posts or Pages -> All Pages from your WordPress dashboard. You will see a new column of post or page IDs.

WordPress Posts screen, highlighting a post ID column.

How to Find WordPress Post IDs and Page IDs: Advanced Methods

Check out the following methods for more technical ways to retrieve post and page IDs.

1. Using WordPress Functions

There are four ways of getting post IDs using WordPress functions.

Get Post ID Using the get_the_ID() and the_ID() Functions

The get_the_ID() and the_ID() functions display the current post’s ID. The main difference between the two functions is that get_the_ID() returns the post ID without displaying it, while the_ID() always prints the post ID.

If you use the get_the_ID() function, make sure to add the echo function to display post IDs:

echo get_the_id();

Meanwhile, the_ID() doesn’t need the echo function to view post IDs:

the_ID();

Get Post ID by Title

The get_page_by_title() function works for WordPress pages and custom post types. This function can be applied to the theme or plugin files.

Here is how the get_page_by_title() function looks like:

$mypost = get_page_by_title( 'post title here', '', 'post' );
echo $mypost->ID;

Make sure to change the ‘post title here‘ parameter with your post title.

Get Post ID by URL

You can also fetch post IDs from WordPress URLs using the url_to_postid() function. This function examines a URL and determines its post ID.

To use the url_to_postid() function, enter the post URL into the function like this:

url_to_postid( 'post’s URL here');

Change the ‘post’s URL here‘ parameter with the post’s URL.

Get Post ID by Slug

Another way to retrieve WordPress post IDs is by using the get_page_by_path() function. This function is similar to the get_page_by_title() function. However, instead of using the post title, you need to enter the post’s slug or path:

$mypost = get_page_by_path('slug here', '', 'post');
echo $mypost->ID;

Remember to replace ‘slug here‘ with your own post slug.

We recommend backing up your WordPress site before testing these functions. We will show you how to use the get_the_ID() function:

  1. From the WordPress dashboard, navigate to Appearance -> Theme File Editor.
  2. Go to the Theme Files tab -> template-parts -> content.php.
WordPress Theme Editor screen, highlighting the content.php file.
  1. Enter the get_the_ID() function under the comment tag.
WordPress Theme Editor Screen, highlighting a code snippet
  1. Click Update File to save the changes.
  2. Click the Visit Site button in the top left corner of the screen. You will see the post ID at the top of each post’s title.
A website's homepage, highlighting two Post IDs.

2. Using the WordPress Posts Table With Custom Code

You can add a post ID column in the WordPress posts table using a custom code. This is an excellent method if you regularly use post IDs:

  1. Open File Manager from your hosting control panel. Alternatively, use a file manager plugin or a theme editor to access website files within the WordPress dashboard.
  2. In File Manager, navigate to public_html -> wp-content -> themes.
  3. Open your active WordPress theme directory. For instance, the active theme is Twenty Sixteen. Consider creating a child theme to minimize the risk of breaking the original theme’s functionality.
  4. Open the functions.php file with a text editor.
  5. Copy and paste the following code snippet at the bottom line of the functions.php file:
function add_column( $columns ){
	$columns['post_id_clmn'] = 'ID'; // $columns['Column ID'] = 'Column Title';
	return $columns;
}
add_filter('manage_posts_columns', 'add_column', 5);

function column_content( $column, $id ){
	if( $column === 'post_id_clmn')
		echo $id;
}
add_action('manage_posts_custom_column', 'column_content', 5, 2);
hPanel File Manager's built-in text editor, highlighting code snippets.
  1. Click Save & Close.
  2. Access your WordPress dashboard and navigate to Posts -> All Post. You will see a new column containing the posts’ ID numbers.
WordPress Posts screen, highlighting a post ID column.

3. Via the WordPress Database

The database stores all necessary WordPress website data, including the IDs of posts, pages, categories, tags, and comments.

To find post IDs within the site’s database, follow these steps:

  1. From your hosting control panel, navigate to Databases -> phpMyAdmin.
  2. Choose a database from the list of current databases and click Enter phpMyAdmin.
  3. In phpMyAdmin, go to wp_posts. Then, locate the ID column on the wp_posts table to see the post IDs.
wp_post table inside phpMyAdmin, highlighting a post ID column.

4. Using the WP_Query Loop

WP_Query is a PHP class in WordPress that lets you load content from the WordPress database. Developers use it to build custom content lists – for example, to display specific posts or pages on the site’s front-end.

To display post IDs using the WP_Query loop, use the following code:

$id_query = new WP_Query( ‘posts_per_page=6’ );

while( $id_query-have_posts() ) : $id_query->the_post();
	$id_query->post->ID;
endwhile;

5. Using the global $post Variable

Getting post IDs using the global $post variable is similar to using the get_the_ID() function. The difference is that the global $post variable is used inside a PHP function. Take a look at the following code:

global $post;
echo $post->ID;

Follow these steps to view the post IDs of all the posts:

  1. Access the WordPress dashboard and navigate to Appearance -> Theme File Editor.
  2. Go to the Theme Files tab -> single.php.
WordPress Theme Editor, highlighting the single.php file
  1. Enter the global $post variable inside the single.php file. The variable is located under the get_template_part() function.
WordPress Theme Editor, highlighting code snippets.
  1. Click Update File to save the changes.
  2. Go to Posts -> All Posts. In the Posts screen, select a post and click View. You will see the current post ID at the bottom of the page.
A website's post, highlighting a post ID.

6. Through the global $wpdb Variable

You can use the global $wpdb variable in a function to get post IDs. In this section, we will cover three ways of using this variable.

Retrieve Post IDs by Meta Key

The global $wpdb variable can fetch a post ID by its meta key. You need to use either an SQL query or a WP_Query query to achieve this.

Take a look at the following code:

/*
 * Returns matched post IDs for a meta key from the 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 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 ) );
 	echo $post->post_id;

	if( count( $ids ) > 1 ) 
		return $ids; // return array
	else
		return $ids[0]; // return int
}

Retrieve Post IDs by Meta Key and Value

Similar to the previous meta key function, but you can also specify a meta value. Here’s what the meta key and value function looks like:

/*
 * Returns matched post IDs for a pair of meta key and meta value from the database
 *
 * @param string $meta_key
 * @param mixed $meta_value
 *
 * @return array|int Post ID(s)
 */
function 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 ) );
	echo $post->post_id;
 
	if( count( $ids ) > 1 ) 
		return $ids; // return array
	else
		return $ids[0]; // return int
}

Retrieve Post IDs by Term ID

You can also use the global $wpdb variable to find a category or tag ID. The following is an example of the term ID function:

/*
 * Returns post IDs in a term with a given ID
 *
 * @param int $term_id
 *
 * @return array|int Post ID(s)
 */
function 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
}

Conclusion

WordPress post and page IDs can help you perform advanced website customizations, such as excluding or including a specific post, creating a custom code, or editing a theme.

There are several ways of finding post IDs and page IDs. Here’s a recap of beginner-friendly ways to get post IDs:

  • Via the URL within the WordPress dashboard
  • Via the URL with the plain permalink structure
  • Using the WordPress editor
  • Using a WordPress plugin

As for the more advanced methods, here’s a recap:

  • Using WordPress functions
  • Using the WordPress posts table with custom code
  • Via the WordPress database
  • Using the WP_Query loop
  • Using the global $post variable
  • Using the global $wpdb variable

Keep in mind that editing WordPress files requires a good understanding of the PHP scripting language. We also recommend backing up your WordPress website before performing any of the more advanced methods.

We hope this article has helped you understand how to find post IDs and page IDs in WordPress. If you have any questions or tips of your own related to the topic, feel free to leave a comment.

Was this tutorial helpful?

Author
The author

Andrew Vickers

Andrew is a passionate WordPress developer. He loves picking apart source code and learning new things. When he’s not working, Andrew likes to hike and play video games.

Author
The Co-author

Blasius Yoga Puspa Krisanta

Yoga is a Digital Content Writer. He has a passion for Information Technology and web development, and is eager to share his experience with readers. During his free time, Yoga makes music and learns to code.