Current page id returns the incorrect value

To get the current page ID outside of The Loop, you need to use get_queried_object_id(); ($wp_query-> is unnecessary).

However, this will only work when you are viewing a single Page (so anything under Pages > All Pages in the back-end).

If you are viewing the blog, an archive, or a category it will not be a valid page ID. On categories it will return the category ID, but on archives or the blog it will not have a value because those things do not have an ID.

If your theme or another plugin uses query_posts(), this could interfere with this function. Using wp_reset_query() will fix this, but whichever theme or plugin is running query_posts() should be doing this.

If you are using query_posts(), don’t. Use a custom query with new WP_Query() or get_posts().