How to Get the Current Page ID Inside a Widget

Here’s a quick tip on how to get the current page ID inside a WordPress widget (outside of The Loop).

With WordPress sometimes you’re not always coding inside The Loop.

Using this little hack allows your widgets to grab the current page ID and could be used to set contextual information which isn’t always easy in WordPress.

Of course it can be used in other parts of your WordPress template too such as footers and asides.

// Get Current Page ID outside Loop
global $wp_query;
$post_obj = $wp_query->get_queried_object();
$Page_ID = $post_obj->ID;
echo $Page_ID;
Was this article helpful?

Keep In Touch


Wil is a dad, WordPress consultant, WordPress developer, business coach and mentor. He co-organizes the WordPress Sydney meetup group and has been on the organising committee for WordCamp Sydney since 2014. He speaks at many special events and contributes to the WordPress open source project. His likes are chillies, craft beer and electrogravitics.

1 thought on “How to Get the Current Page ID Inside a Widget”

Comments are closed.