Your current code relies on time travel to work:
$IDOutsideLoop = $post->ID;
global $post;
Specifically the first line is peering into the future, how can it know what $post->ID
is if it’s undeclared? The fix is global $post;
, but when the computer gets to that line, it’s too late
global $post;
$IDOutsideLoop = $post->ID;
If you want to list posts, you should consider WP_Query
or get_posts
. It may be faster in your case to use pre_get_posts