Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

I could not find any documentation on $post_id.

share|improve this question
add comment

1 Answer

up vote 2 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
add comment

Your Answer

 
discard

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.