WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is the difference between $post_id and get_the_ID()?

I could not find any documentation on $post_id.

share|improve this question
up vote 8 down vote accepted

get_the_ID() returns the ID of the current post by way of get_post. Get post assumes the current post to be the global variable $post unless passed parameters specifying otherwise. That means that get_the_ID() should return the same value as contained in $post->ID. Using get_the_ID() lets you avoid dealing with the global directly, though you are dealing with it indirectly. Using get_the_ID() tends to be less error-prone in theme template files.

$post_id is a variable name used commonly to refer the post ID, but it isn't a Core variable the way that $post is, though it does get defined a few times in specific contexts:

grep -Rn '$post_id' * |grep global
wp-admin/includes/class-wp-comments-list-table.php:25:          global $post_id;
wp-admin/includes/class-wp-comments-list-table.php:45:          global $post_id, $comment_status, $search, $comment_type;
wp-admin/includes/class-wp-comments-list-table.php:141:         global $post_id, $comment_status, $comment_type;
wp-admin/includes/class-wp-comments-list-table.php:250:         global $post_id;
wp-admin/includes/ajax-actions.php:691: global $wp_list_table, $post_id;
wp-includes/ms-deprecated.php:115:function clear_global_post_cache( $post_id ) {
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.