WordPress at Your Fingertips

the_ID() WP 0.71

Display the ID of the current post in the WordPress Loop.

Post ID in the loop can be also obtained through $post->ID. Sometimes it's easier and there is no unnecessary call of the function.

1 time — 0.000403 sec (fast) | 50000 times — 0.18 sec (very fast) | PHP 7.1.2, WP 4.7.3

No Hooks.


null. Nothing (null). The post ID is displayed immediately.


<?php the_ID(); ?>


#1 Let's print the inscription "Post Number: 465" on the screen (for post 465)

<p>Post Number: <?php the_ID(); ?></p>

#2 HTML Anchor of the post

<h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>

Now the link to this anchor will be: http://site.com/link_to_post#post-465

in XHTML, the id attribute of an HTML tag cannot start with a digit. Therefore, you need to add at least one letter before the_ID().


Since 0.71 Introduced.

Code of the_ID() WP 5.8

function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	echo get_the_ID();

From tag: WP Loop

More from category: Posts, pages...

More from Template Tags: Posts, Pages, ...

vladlu 100vlad.lu
Editors: Kama 100
No comments
    Log In