Get WordPress post ID inside and outside of the loop

Getting the post ID comes in handy when writing conditional logic.

Post ID inside loop

As simple as:

$post->ID;

Sometimes, when you're in a customized loop, I find that the is_page($pageid) doesn't work. In that case, it comes in handy to do something like this:

$post_id = $post->ID;
if (post_id == 1190) {
  // Do something if true
} else {
  // Do something else if false
}

Post ID outside the loop

global $wp_query; needs to be declared before getting the ID:

global $wp_query;
$post_id = $post->ID;

That's it :)