Title: get_the_ID
Published: April 25, 2014
Last modified: February 24, 2026

---

# get_the_ID(): int|false

## In this article

 * [Return](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#wp--skip-link--target)

Retrieves the ID of the current item in the WordPress Loop.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#return)󠁿

 int|false The ID of the current item in the WordPress Loop. False if $post is not
set.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#source)󠁿

    ```php
    function get_the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
    	$post = get_post();
    	return ! empty( $post ) ? $post->ID : false;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post-template.php#L27)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post-template.php#L27-L30)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#related)󠁿

| Uses | Description | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

| Used by | Description | 
| [print_embed_sharing_dialog()](https://developer.wordpress.org/reference/functions/print_embed_sharing_dialog/)`wp-includes/embed.php` |

Prints the necessary markup for the embed sharing dialog.

  | 
| [wp_dashboard_recent_posts()](https://developer.wordpress.org/reference/functions/wp_dashboard_recent_posts/)`wp-admin/includes/dashboard.php` |

Generates Publishing Soon and Recently Published sections.

  | 
| [wp_get_archives()](https://developer.wordpress.org/reference/functions/wp_get_archives/)`wp-includes/general-template.php` |

Displays archive links based on type and format.

  | 
| [get_post_comments_feed_link()](https://developer.wordpress.org/reference/functions/get_post_comments_feed_link/)`wp-includes/link-template.php` |

Retrieves the permalink for the post comments feed.

  | 
| [prepend_attachment()](https://developer.wordpress.org/reference/functions/prepend_attachment/)`wp-includes/post-template.php` |

Wraps attachment in paragraph tag before content.

  | 
| [the_ID()](https://developer.wordpress.org/reference/functions/the_id/)`wp-includes/post-template.php` |

Displays the ID of the current item in the WordPress Loop.

  | 
| [wp_video_shortcode()](https://developer.wordpress.org/reference/functions/wp_video_shortcode/)`wp-includes/media.php` |

Builds the Video shortcode output.

  | 
| [wp_audio_shortcode()](https://developer.wordpress.org/reference/functions/wp_audio_shortcode/)`wp-includes/media.php` |

Builds the Audio shortcode output.

  | 
| [is_sticky()](https://developer.wordpress.org/reference/functions/is_sticky/)`wp-includes/post.php` |

Determines whether a post is sticky.

  | 
| [get_post_custom()](https://developer.wordpress.org/reference/functions/get_post_custom/)`wp-includes/post.php` |

Retrieves post meta fields, based on post ID.

  | 
| [wp_list_comments()](https://developer.wordpress.org/reference/functions/wp_list_comments/)`wp-includes/comment-template.php` |

Displays a list of comments.

  | 
| [get_trackback_url()](https://developer.wordpress.org/reference/functions/get_trackback_url/)`wp-includes/comment-template.php` |

Retrieves the current post’s trackback URL.

  | 
| [comments_popup_link()](https://developer.wordpress.org/reference/functions/comments_popup_link/)`wp-includes/comment-template.php` |

Displays the link to the comments for the current post ID.

  |

[Show 8 more](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 6 content](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#comment-content-1890)
 2.    [Craigbavender](https://profiles.wordpress.org/craigbavender/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/get_the_id/#comment-1890)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-1890)
     Vote results for this note: 12[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-1890)
 4.  Should be noted that if this is ran on the Blog homepage, it instead returns the
     first listed Post ID instead of the blog homepage ID.
 5.   * If you are looking for conditional checks (i.e. if its this page, do this),
        [is_single()](https://developer.wordpress.org/reference/functions/is_single/)
        checks if its the single post version of an ID. There are also other for homepage,
        page, etc. Check the other conditionals for others: [https://codex.wordpress.org/Conditional_Tags](https://codex.wordpress.org/Conditional_Tags)
      * [michaeldhayes](https://profiles.wordpress.org/michaeldhayes/) [5 years ago](https://developer.wordpress.org/reference/functions/get_the_id/#comment-4668)
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%3Freplytocom%3D1890%23feedback-editor-1890)
 7.   [Skip to note 7 content](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#comment-content-3767)
 8.    [Joy Chetry](https://profiles.wordpress.org/joychetry/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/get_the_id/#comment-3767)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-3767)
     Vote results for this note: 9[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-3767)
 10. In some cases, such as when you’re outside The Loop, you may need to use
      [get_queried_object_id()](https://developer.wordpress.org/reference/functions/get_queried_object_id/)
     instead of [get_the_ID()](https://developer.wordpress.org/reference/functions/get_the_id/).
 11.     ```php
         $postID = get_queried_object_id();
         ```
     
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%3Freplytocom%3D3767%23feedback-editor-3767)
 13.  [Skip to note 8 content](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#comment-content-300)
 14.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/get_the_id/#comment-300)
 15. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-300)
     Vote results for this note: 7[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-300)
 16. **Post Anchor Identifier**
      `get_the_ID()` can be used to provide a unique anchor
     in a script. For instance, a dynamically-generated drop down menu with actions
     for each post in an archive could have
 17.     ```php
         <?php
         	$id = get_the_ID();
         	$dropdown = "<select name='dropdown-".$id."' >";
         	$dropdown .= "<option id='option1-". $id ."'>Option 1</option>";
         	$dropdown .= "</select>";
         ?>
         ```
     
 18. This would allow us to use JavaScript to control the element as it has a unique
     ID, and when submitting it as a form through the POST or GET methods the dropdown
     box will be sent with a unique ID which allows the script to note which post it
     is working on. Alternatively a hidden variable could be sent which will allow 
     the script to see which post the submission is referring to
 19.     ```php
         <?php
         	echo '<input type="hidden" name="activepost" id="activepost" value="'.get_the_ID().'" />';
         ?>
         ```
     
 20.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%3Freplytocom%3D300%23feedback-editor-300)
 21.  [Skip to note 9 content](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#comment-content-6361)
 22.   [tradesouthwest](https://profiles.wordpress.org/tradesouthwestgmailcom/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/get_the_id/#comment-6361)
 23. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-6361)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-6361)
 24. If you are working with custom post types or you are just not sure if the file
     you are working in has any direct access to the post, you can try this handy statement
     to get the ID of a post; outside of the loop even.
 25.     ```php
         global $post;  
             $post_id = ( empty( $post->ID ) ) ? get_the_ID() : $post->ID;
         ```
     
 26. Then use $post_id string to assure you have the post ID. Example usage:
 27.     ```php
         if ( get_post_type( $post_id ) != 'wpbdp_listing' ) return;
         ```
     
 28.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%3Freplytocom%3D6361%23feedback-editor-6361)
 29.  [Skip to note 10 content](https://developer.wordpress.org/reference/functions/get_the_id/?output_format=md#comment-content-299)
 30.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/get_the_id/#comment-299)
 31. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-299)
     Vote results for this note: -1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%23comment-299)
 32. **Store the ID**
      The ID can be stored as a variable using
 33.     ```php
         <?php $postid = get_the_ID(); ?>
         ```
     
 34.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F%3Freplytocom%3D299%23feedback-editor-299)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_id%2F)
before being able to contribute a note or feedback.