Current page ID Inside or Outside the loop in WordPress

In WordPress we’re not always working inside the WordPress loop. Working inside the loop, we use the template tag the_ID, which easily get current page ID. But while working outside the loop then getting current page ID is bit tricky.

Get Current Page ID Inside The Loop

$page_id = the_ID();
return numerical ID of the current post or page in the WordPress Loop. False if $post is not set.

Get Current Page ID Outside The Loop

Let’s look at how this is done. To get page id outside the loop we can make use of get_queried_object() here, which is a wrapper for $wp_query and returns the whole post metadata.

global $wp_query; 
$page = $wp_query->get_queried_object(); 
$page_id = $page->ID; 

$page_id = get_queried_object_id();

About This Author

My name is Parameshwar Roy (P. Roy), and I am a web developer. This is my personal blog to record my own thoughts. Though I am not a natural writer, I love to share my experiences. Hope my experiences will be useful to more about me

1 Comment

You can post comments in this post.

  • I want to display the current post inside my custom plugin. pls, give some solution ..

    Tapan Ghosh 1 year ago Reply

Leave A Reply