$post->ID displays wrong ID

On that page $post->ID returns the ID of first blog post for given
page.

That is how it works. $post is set to the first post in the Loop. On single posts and pages that is the same as the post or page. On archive pages it is the first post in the result set. And if you think about that, both are really the same thing. Single posts and pages only have one result in the set which happens to match the post or page that you expect.

Now, the tricky part, I’ve tried to remove all the loops from all my
pages, totally erased loop.php file, disabled all the widgets that
might have been affecting $post and it still returns the wrong ID.

The main query runs before your template loads and $post is set in that process. Removing things from the template won’t change that.

Any hints?

Yes. Don’t rely on $post except inside a proper Loop. If you need information about the page itself use:

$pobj = get_queried_object();
var_dump($pobj); // debugging

Reference:

http://codex.wordpress.org/Function_Reference/get_queried_object

Leave a Comment