Current page ID Inside or Outside the loop in WordPress

  • Update On January 15th, 2017
  • in WordPress

This post was last updated on January 15th, 2017 at 06:04 pm

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; 

//OR
$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 you...read more about me

2 Comments

You can post comments in this post.


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

    Tapan Ghosh 5 years ago Reply


  • Waʏ cool! Some extremely valid ρoints! I aρpreciate you penning this ᴡrite-uр and thе rest ߋf the site
    is aⅼѕo rеally gߋod.

    Associated 4 years ago Reply


Leave A Reply