WordPress LogoYet another of those “it took me forever to find the answer so I figured I’d post it” posts.

The common answer to the question of “how do I get the ID of the current page?” is


But that fails when the page ID you’re looking for is actually the Posts page. In that case the above statement returns the ID of the first post, not the page itself. Really annoying.

The more bulletproof solution (which works in all cases) is

global $wp_query;
$page_id = $wp_query->get_queried_object_id();

Hope that helps someone other than me (or future me).