WordPress the_ID() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints the post ID

Definition

In WordPress, the_ID() prints the ID number of the current post. Every post has a unique ID, such as 12.

It is often used to build a unique id attribute for each post in The Loop.

Syntax

A few ways to call the_ID().

WordPress
<article id="post-<?php the_ID(); ?>">
CallWhat it does
the_ID()Print the ID of the current post.
get_the_ID()Return the ID as a number, or false.

It prints the ID and takes no arguments. Use get_the_ID() when you need the number in code.

Full declaration
WordPress
the_ID(): void

Parameters

ParameterDescription
(none)This function takes no parameters.

Return Value

Return ValueDescription
voidNothing is returned. The ID is printed.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Make a unique ID

Give each post a unique id attribute.

WordPress
<?php while ( have_posts() ) : the_post(); ?>
  <article id="post-<?php the_ID(); ?>">
    <h2><?php the_title(); ?></h2>
  </article>
<?php endwhile; ?>

How It Works

  1. the_ID() prints the ID of the current post.
  2. Each post gets a different id.
  3. You can now link to #post-12 or style it.

Example 2 — Use get_the_ID() in code

Use the return version when you need the number.

WordPress
<?php
$post_id = get_the_ID();
echo 'Post ID: ' . intval( $post_id );

How It Works

  1. get_the_ID() returns the ID instead of printing.
  2. intval() makes sure we print a whole number.
  3. You can pass the ID to other functions.

Example 3 — Read a custom field

Use the ID to read a custom field.

WordPress
<?php
$mood = get_post_meta( get_the_ID(), 'mood', true );
echo esc_html( $mood );

How It Works

  1. get_the_ID() gives the current post ID.
  2. get_post_meta() reads the custom field named mood.
  3. esc_html() makes the value safe.

Frequently Asked Questions

It prints the ID number of the current post.
the_ID() prints the ID. get_the_ID() returns it.
It is the_ID() with capital letters. PHP function names are not case sensitive, but WordPress uses this spelling.
Yes. It uses the current post.
Hover over the post in the posts list, or look at the URL when you edit it. It shows post=12.

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful