I’m a bit surprised the answer to this question is so difficult to find. Or maybe I don’t know where/how to look. I’ve tried the following function, but it’s not working for in my widget (though it does work for me directly when I hard code it into sidebar.php).
`// Works in single post outside of the Loop
function function_name() {
global $wp_query;
$thePostID = $wp_query->post->ID;
}`