get_the_ID() in the footer returns wrong value

I'm using get_the_ID() in footer.php but it returns a wrong value.

It might come from any of the plugin which is altering The Loop without resetting it after with wp_reset_postdata() as mentioned in $post-ID and the_id() always return the wrong value.

Indeed the page shows a list of recent posts, etc. so this might modify the current Loop.

If I can't modify this (because of third party code), how to get the ID of the current post in the footer anyway, with PHP?

Topic get-the-id id loop wp-query php Wordpress

Category Web


In an environment without third party code, get_the_ID() should give you the correct value. It sounds like some plugin is interfering with this.

So you can do what they forgot and simply call

wp_reset_postdata()

in your footer before you need the values.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.