How to get current page ID outside the loop?

  • 4
    Strange, how are you using the code, what have you tried. What errors are you getting. As it stands, it is difficult to help you and all answers is a shot in the dark. Please file an edit Commented Sep 17, 2014 at 14:06
  • <?php global $post; $thePostID=$post->ID;?> <?php elseif($thePostID==35) : ?> Commented Sep 17, 2014 at 14:12
  • 3
    Please file an edit with all the extra code/info. Don't use the comment section for stuff that should be in your question :-) Commented Sep 17, 2014 at 14:14
  • Your post has been automatically flagged as low quality, please comply to my requests above :-) Commented Sep 17, 2014 at 14:15
  • 1
    As I said, all of that info should go into your question, not in the comments section. Please edit your question Commented Sep 17, 2014 at 14:19

5 Answers 5



global $post;
echo $post->ID;

or (I don't know the difference)

global $wp_query;
echo $wp_query->post->ID;
  • i have tried all the stuff found on google but nothing happened Commented Sep 17, 2014 at 14:01
  • it's hard to know what "all the stuff you found" is :) make sure the code is in a function, that you call that function and start debuging with echoing something else before which has to be visible...
    – caramba
    Commented Sep 17, 2014 at 14:04
  • 2
    i did't getting id using function, and the "stuff" is global $wp_query; $thePostID = $wp_query->post->ID; global $post; $thePostID = $post->ID; global $wp_query; $page = $wp_query->get_queried_object(); $page_id = $page->ID; $page_id = get_queried_object_id(); Commented Sep 17, 2014 at 14:09
  • first one was correct... Commented Sep 17, 2014 at 14:39
  • 3
    These solutions will give you page ID only if used before the Loop starts. Once the Loop starts, $post will be set to each post in the Loop in turn.
    – s_ha_dum
    Commented Aug 19, 2015 at 13:59

You can simply do,

$page_id = get_queried_object_id();
  • 10
    This should be marked as the answer...
    – Lezz
    Commented Jan 28, 2020 at 9:59
  • 7
    Agreed. This should be the answer.
    – CJWEB
    Commented Mar 8, 2020 at 4:44
  • Definately it should be the right answer. I'm trying to get the current id of a post or a page, and this is the only way it gives me the right Id without having to hardcode conditions. Commented May 22, 2020 at 15:38
  • Brilliant, right answer for me Commented Nov 19, 2020 at 22:41
  • Thank you. Great answer. Commented Jun 7, 2021 at 15:05

For those of you who this still isn't working for, you will need to use some sort of add_action (you can choose which one you want to use). For my example, this will return the current page ID without any problems, regardless of whether in a plugin folder, functions php, or elsewhere.

add_action('template_redirect', 'showid');

function showid(){
    global $wp_query;
    $theid = intval($wp_query->queried_object->ID);
    echo $theid;

Good luck and happy coding!

  • Why was this answer downvoted?
    – crs1138
    Commented Oct 22, 2021 at 10:09

Outside the loop:

$current_page = get_page_by_path($_SERVER['REQUEST_URI']);
if(isset($current_page->ID)) {
    $current_page_id = $current_page->ID;
  • That looks like it should work, but I think the other approaches are simpler. And I'm nervous this might make a database request.
    – Rup
    Commented Jun 29, 2022 at 21:51
  • This will not work when creating a new page.
    – Tahi Reu
    Commented Apr 7, 2023 at 9:42

Outside the loop :

  //remember to call wp_reset_postdata(); after while loop
  // otherwise you will get wrong id.
   global $post;
   $post_id =  $post->ID;

Inside the loop:

while (have_posts()) {
    $post_id = the_ID();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.