WordPress

7 Easy Steps to Getting Post and Page IDs in WordPress

Photo of author

By Admin Desk

Post and page IDs are vital for helping you organize your WordPress website better. They’re needed when coding your website and when working with relevant plugins, themes, and widgets.

However, the content management system wants you to put a little effort into fetching the IDs. From identifying the edit page URL and modifying PHP files to going into the website database and activating an ID-reveal plugin. There are numerous ways to get your post and page IDs.

One of the easiest methods to get an ID is by checking out your site’s database. For this, make sure your hosting provider grants phpMyAdmin access from its dashboard or control panel, like  WordPress hosting Hostinger does.

This article will show you the seven quick and easy steps to getting post and page IDs in WordPress. But before we dive into the tutorial, let’s define what post and page IDs in WordPress are.

What Are Post and Page IDs in WordPress?

IDs in WordPress are unique identification numbers for each of your website files, including:

  • Posts
  • Pages
  • Media files
  • Categories

Those identity numbers help the CMS organize and keep track of the published, drafted, and deleted files on your WordPress website.

To your advantage, IDs help you specify a post for a query. Suppose you want to add a custom code for a specific post on your WordPress plugin and theme’s function. Then, you can just embed the post’s ID to the extension’s code.

How To Get Post and Page IDs?

Post and Page IDs may take some effort to find. By learning how to identify an ID, managing your website can become much easier. In this section, let’s look at the five ways of getting post and page IDs.

1. Identify the Edit Page’s URL

Identifying the edit page’s URL is one of the simplest ways to locate a post’s ID. All you have to do is:

  1. Go to your WordPress dashboard.
  2. Select Posts -> All Posts.
  3. Hover over the post title.
  4. Choose Edit.

Choose Edit

While editing, have a look at the edit page’s URL. On it, you’ll find the ID number after the equal sign.

Or, hover over the Edit button and check the post URL on the bottom left of your screen.

You can also use these two tricks to get your page IDs. All you have to do is access your Page tab and hover over the Edit button or go to the edit page.

Edit Page Button

2. Explore Your WordPress Site’s Database

Your WordPress site’s database has all of your information. Therefore, it’s one of the best places to find your page and post IDs. For that, follow the easy instructions below:

  1. Log in to your web host’s dashboard.
  2. Locate the Databases section.
  3. Select phpMyAdmin -> Enter phpMyAdmin.
  4. Choose the wp_posts folder.

Once done, you’ll see a tab containing all of your WordPress site’s pages and posts, like so:

3. Use get_the_id and the_id Functions

The Hypertext Preprocessor (PHP) is an open-source server-side scripting language. PHP builds the core WordPress software, making it an essential language to learn for WordPress users.

Fetch your page’s and post’s IDs by modifying your PHP with these two simple strings of code below.

First, you have a function that returns the current page or post’s ID while it’s executed:

get_the_id();

Second, the function below prints the current page or post’s ID. Meaning, this function will display your post ID in the HTML output. Here’s what it may look like:

the_id();

4. Utilize the Post Title

This method also requires you to play around with your site’s PHP files.

$mypost = get_page_by_title( ‘post title’, ’’, ‘post’ );

$mypost->ID

The above code retrieves post and page ID by its title, and it won’t display the number in the HTML output. Suppose you have multiple posts with similar names. In that case, the code will return the post with the smallest ID number.

5. Take Advantage of the WP_Query Loop

The WP_Query allows developers to pull posts from the website database based on specific criteria. That said, the WP_Query can help you find a page and post ID as well.

Here’s how you get IDs using the WP_Query loop:

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

while( $id_query-have_posts() ) : $id_query->the_post();

$id_query->post->ID;

endwhile;

6. Add an ID Column in the Post and Page Tables

The default post table doesn’t have an ID column. Here’s what it looks like on your WordPress dashboard:

Add an ID Column in the Post and Page Tables

By inserting a string of code to your active theme’s function.php, you can get a post ID column in that table. Here are the steps to do it:

  1. Use a File Transfer Protocol, like FileZilla.
  2. Fill in your website’s FTP client credentials.
  3. Once verified, double-click the wp-content folder.
  4. Open the Theme folder.
  5. Locate your activated theme folder.
  6. Launch the function.php files.

Once you’re inside, add the strings of code below:

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);

Don’t forget to save when you’re finished. Finally, here’s how your Post table should look like now:

save after adding

7. Activate an ID-Reveal Plugin

If tweaking PHP code is too confusing for you, install a plugin to get your page and post IDs. Some of the best plugins include Show IDs by 99 Robots and Catch IDs. To add the plugin to your WordPress website, follow the easy steps below.

Activate an ID-Reveal Plugin

  1. Through your WordPress dashboard, navigate to Plugins -> Add New.
  2. Type in the plugin keyword onto the search bar.
  3. Click the Install Now button.
  4. Click Activate to launch the plugin.

Now, go to your post or page table and see if they have the ID columns. Here’s what it should look like on your Page tab.

WordPress ID Columns

Conclusion

Post and page IDs are unique identification numbers that help you target a specific post to query. Although WordPress doesn’t explicitly give IDs away for its users, there are many ways to get them.

Throughout this post, you’ve learned the seven easy ways to spot your post and page IDs, which can be cut down into four big groups:

  • Fetching IDs through URL.
  • Going into the WordPress database.
  • Executing some strings of code to the PHP files.
  • Activating an ID-reveal plugin.

Installing a plugin is the easiest way to get your post and page IDs. However, you can dip your toes into a bit of coding by modifying your PHP files. Whichever method you choose, we wish you good luck in finding those IDs!