get_the_ID() | Function – WordPress

The get_the_ID() function in WordPress is used to retrieve the ID of the current post in the loop. The function returns the ID of the post as an integer.

Here’s an example of how you could use the get_the_ID() function to display the ID of the current post:

scss
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
echo 'The current post ID is: ' . get_the_ID();
}
}

In this example, the have_posts() function is used to check if there are any posts to display, and the the_post() function is used to iterate through the posts. Inside the loop, the get_the_ID() function is called to retrieve the ID of the current post, which is then displayed.

The get_the_ID() function is useful when you need to retrieve the ID of the current post in the loop and use it in some way, such as to retrieve custom field data, display information about the post author, or perform other actions based on the post ID.

Note that the get_the_ID() function must be used within the loop. If you need to retrieve the ID of a post outside of the loop, you can use the get_post() function instead, which allows you to specify the ID of the post you want to retrieve.