0

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?

4
  • What happens if you call wp_reset_postdata() in your footer before the get_the_ID()?
    – kero
    Commented Dec 2, 2021 at 16:12
  • Yes it works @kero but I wanted to know if there is another less intrusive method (than resetting the postdata).
    – Basj
    Commented Dec 2, 2021 at 16:19
  • 1
    Why would it be intrusive? get_the_ID() should return the correct ID. Now most likely some third party code is not behaving nicely, and with calling wp_reset_postdata() you simply reset the state to how it should have been. If something breaks, get rid of that what broke (and whatever forgot to reset the postdata).
    – kero
    Commented Dec 2, 2021 at 16:40
  • Thanks @kero, I think you can post as an answer!
    – Basj
    Commented Dec 2, 2021 at 16:45

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.