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 ) {
get_the_ID()
returns the ID of the current post by way ofget_post
. Get post assumes the current post to be theglobal
variable$post
unless passed parameters specifying otherwise. That means thatget_the_ID()
should return the same value as contained in$post->ID
. Usingget_the_ID()
lets you avoid dealing with the global directly, though you are dealing with it indirectly. Usingget_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: