How to Get WordPress Post ID

Getting the post ID in WordPress is useful for many reasons. As a blogger or website owner, you may need the ID to configure plugins or apply custom code. As a developer, post IDs allow you to target specific content to add functionality.

In this comprehensive guide, we will cover five easy methods to find post IDs in WordPress.

Why Get the Post ID in WordPress

Every piece of content in WordPress, including posts, pages, attachments, categories, tags, etc. has a unique ID assigned to it.

Here are some common reasons you may need to access the post ID:

  • Configure plugins – Many plugins allow you to include/exclude posts based on ID.
  • Develop custom code – Add custom CSS, JavaScript, PHP code targeting specific posts.
  • Build custom queries – Retrieve or filter content based on post ID.
  • Improve SEO – Use ID to apply focussed optimisation for key posts.
  • Troubleshoot issues – Identify content causing problems using the ID.

So whether you just need it occasionally or routinely, it’s good to know how to find post IDs.

Method 1: Check the Post URL

The easiest way to find the post ID is by checking the post URL:

  1. Go to Posts > All Posts in your WordPress dashboard.
  2. Hover over a post title and click Edit.
  3. Look at the URL in your browser’s address bar.

The post ID is the number that comes after post= in the URL.

For example:

http://example.com/wp-admin/post.php?post=1548&action=edit

Here the post ID is 1548.

This method works for posts, pages, and custom post types.

Method 2: Use a Plugin

If you need to routinely access post IDs, using a plugin like Show IDs can save time.

This plugin adds an ID column to various sections like Posts, Pages, Categories, Tags, etc. making IDs easily visible.

To use:

  1. Install and activate the Show IDs plugin.
  2. Go to Posts > All Posts.
  3. You will now see the ID column with post IDs.

Method 3: Check the WordPress Database

The WordPress database stores all site content including the post IDs.

To find IDs in the database:

  1. Access phpMyAdmin through your hosting control panel.
  2. Open your WordPress database.
  3. Go to the wp_posts table.
  4. The post ID is under the ID column.

This method allows you to view IDs for all post types, not just posts and pages.

Method 4: Use Built-In WordPress Functions

As a developer, you can also retrieve IDs using these WordPress functions:

  • get_the_ID() – Get ID for the current post in loop.
  • get_post_field( ‘ID’) – Get ID for a specific post object.
  • url_to_postid() – Get ID from the post URL.

For example:

$current_post_id = get_the_ID(); 
$post_id = get_post_field( 'ID', $post_object );

So without having to hard code IDs, you can easily get them through code.

Method 5: Check Media Attachment Details

The process to get media attachment IDs is slightly different but easy.

To find an attachment ID:

  1. Go to Media > Library.
  2. Click the attachment to open details.
  3. Check the URL for the ID number.

The structure will be like:

http://example.com/wp-admin/upload.php?item=1522

Where 1522 is the media attachment ID.

Wrap Up

Finding post IDs in WordPress is easy once you know where to look.

As you can see, there are many methods – from using your dashboard to installing plugins to writing custom code.

Some key takeaways:

  • Post IDs allow you to uniquely identify pieces of content.
  • They are useful for developing sites and configuring plugins.
  • You can retrieve IDs from the post URL, database, in-built functions, etc.
  • Media attachments have IDs stored similarly.

So next time you need to get a post ID in WordPress, use one of these five simple methods.