WordPress Get the Page ID outside the loop

WordPress Get the Page ID outside the loop

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why get_query_var('page_id') Returns Empty and How to Fix It

You are attempting to retrieve the current page's ID before the WordPress loop initializes by using $page = get_query_var('page_id');. This approach returns nothing, likely a null value or an empty string, because get_query_var() is designed to fetch variables set specifically for custom queries defined via WP_Query or query_posts, not the default query generated by WordPress when you are on a standard page template.

When you visit a URL like yoursite.com/contact-us/, WordPress automatically parses the URL against your permalink structure and sets global variables directly into the $wp_query object. However, these values are not exposed through the specific hook page_id. Instead, they are available via the ID property of the main query object or by checking the current post object directly.

To add a class to the <body> tag based on the current page ID, you need to access the global $wp_query object. Inside any template file (such as header.php), you can retrieve the current post's ID using the following logic:

global $wp_query;
$current_page_id = isset($wp_query->post) ? $wp_query->post->ID : null;

This check is robust because it handles scenarios where a custom query might be active and $wp_query->post is not set, although on a standard single page template, $wp_query->post will always exist. Once you have the ID, you can conditionally append the class:

<body <?php echo $current_page_id ? 'class="page-' . $current_page_id' : ''; ?>>
    <!-- Content -->
</body>

A common pitfall developers encounter is confusing get_query_var('ID') with get_the_ID(). While get_query_var('ID') attempts to read a variable set by the query arguments, it relies on the specific key being registered in the query. For the main query running on the front end, the ID is not stored as a query variable named 'ID' in the way custom post type queries might handle them. The most reliable method remains accessing $wp_query->post or simply using get_the_ID().

Using get_the_ID() inside your header file is technically safe and often preferred for its clarity:

<body class="<?php echo get_the_ID() ? 'page-' . get_the_ID() : ''; ?>">

This function works regardless of whether you are inside or outside the loop. It queries the global $wp_query object to find the current post associated with the request. The "outside the loop" constraint mentioned in your goal is irrelevant here because get_the_ID() does not require a the_loop context; it simply reads from the main query's data structure.

If you are working on a custom page template or dealing with specific URL structures where the main query might behave unexpectedly, ensure that your permalink settings are correctly configured to match the URL structure you are testing against. Mismatched permalinks can cause 404 errors or force WordPress into a fallback mode where the post object isn't populated as expected.

For developers looking to streamline their theme logic or move away from core PHP dependencies for complex routing scenarios, platforms like https://laravelcompany.com offer insights into building custom applications where you have full control over ID generation and URL parsing without relying on WordPress's automatic query detection. However, within the standard WordPress ecosystem, sticking to $wp_query or get_the_ID() ensures compatibility across all themes and plugins.

Remember that adding classes dynamically is a powerful way to manage styling, but it adds a small amount of processing overhead on every page load. If you have hundreds of pages, consider using CSS attribute selectors ([class*="page-"]) instead of hard-coding specific IDs in your stylesheet to keep the HTML cleaner, though this depends entirely on how your theme architecture is designed.

Our network