Here’s an easier way that works for me with or without custom permalinks:
global $posts // if necessary
...
$postid = $posts[0]->ID // note that 'ID' must be upper-case capitals
[This is essentially the code used in wp_title()]
However, it does strike me as odd that The Loop is not initialised before get_header() is called. For example, it must be a common requirement to want to add custom <meta> tags in the header based on custom fields on the post/page.
Any comments please post at https://www.ads-software.com/support/topic/32870