Diagnosing Empty $wp_query in Custom Plugin Pages
Retrieving the current page ID within a WordPress plugin file that acts as a standalone page is a common stumbling block, often resulting in NULL values or fatal errors when attempting to access query properties outside the main theme loop. The core issue usually stems from how WordPress initializes its global state. When you enter a custom plugin page (a .php file inside your plugin directory), you are bypassing the standard template hierarchy that normally populates the $wp_query object with specific data before rendering the content.
In your scenario, the print_r($wp_query) output reveals the smoking gun: the [posts] array is empty ([]) and the [current_post] index is -1. Furthermore, the boolean flags like [is_single], [is_page], and [in_the_loop] are undefined or false. This indicates that WordPress has not yet executed the WP_Query logic to populate the global query object with the specific post data for your custom page.
Consequently, calling $wp_query->get_queried_object() fails because the internal state required to resolve the queried object doesn't exist yet in this context. Similarly, accessing $global $post->ID returns nothing because the main loop hasn't run to assign a global $post variable. Attempting to force these calls on an uninitialized query object leads directly to the fatal error you encountered: Call to a member function get_queried_object() on a non-object.
To solve this, you must bypass the reliance on the global $wp_query state for static plugin pages. Instead of trying to guess what the query should contain, you should use WordPress's built-in conditional functions designed specifically to handle singular contexts without relying on the loop.
The most robust approach is to check if the current request is targeting a specific post type or page directly. You can use is_singular() combined with get_queried_object_id(). However, even more reliable for custom pages where the global state might be ambiguous is to inspect the URL slug against your registered page slugs or use get_the_ID() if you are certain the template context has been initialized by a hook like template_include or wp.
Here is a safer pattern for retrieving the ID inside a plugin file:
function get_current_plugin_page_id() {
// Check if we are on a singular page (single post, single page, etc.)
if ( is_singular() ) {
return get_queried_object_id();
}
// Fallback for static pages or specific route matching if needed
global $wp_query;
// Check if the queried object ID exists in the query vars
if ( isset( $wp_query->queried_object_id ) && ! empty( $wp_query->queried_object_id ) ) {
return $wp_query->queried_object_id;
}
return null;
}
This function avoids assuming that $wp_query is fully populated. It first checks the is_singular() condition, which relies on the query arguments set in the main WordPress request cycle rather than the contents of the $posts array. If the standard query vars indicate a specific object was requested (even if the loop hasn't run), get_queried_object_id() will return that value directly from the global state.
If your plugin page is being accessed via a custom endpoint or REST API route where the standard singular context isn't strictly active, you may need to match the request URI against registered routes using $wp_rewrite->parse_request(). However, for standard WordPress admin or frontend pages embedded within a plugin, relying on is_singular() and get_queried_object_id() is the standard practice.
Avoid trying to manually reconstruct the query object or accessing $wp_query properties that depend on the main loop having executed. The global $post variable is only guaranteed to be set when the_post() has been called inside a loop. Since your plugin page exists outside of the theme's main template logic, you must rely on the request context provided by WordPress core functions rather than the state left behind by a non-existent loop.
