As others figured already out: If you have third party plugins with custom queries, you’ll have a bad day!
One solution might be caching your results.
Template (e.g.: page.php)
while (have_posts()): the_post();
global $my_cached_data;
$post_id = get_the_ID();
/*
* If you have single meta keys, this array_map function
* makes them easy to access; otherwise just use:
*
* $my_cached_data[$post_id] = get_post_meta( $post_id)
*/
$my_cached_data[$post_id] = array_map(
function( $a )
{
return $a[0];
},
get_post_meta( $post_id)
);
endwhile;
footer.php
global $my_cached_data;
echo "<pre>".print_r($my_cached_data, true)."</pre>";
Possible Result (with Page ID 2 and 15 having the same post meta data)
array(2) {
[2]=>
array(2) {
["meta_key"]=>
string(10) "meta_value"
["another_key"]=>
string(10) "meta_value"
}
[15]=>
array(2) {
["meta_key"]=>
string(10) "meta_value"
["another_key"]=>
string(10) "meta_value"
}
}