52

How to get current page ID outside the loop?

9
  • 3
    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 Sep 17, 2014 at 14:06
  • <?php global $post; $thePostID=$post->ID;?> <?php elseif($thePostID==35) : ?> 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 :-) Sep 17, 2014 at 14:14
  • Your post has been automatically flagged as low quality, please comply to my requests above :-) 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 Sep 17, 2014 at 14:19

5 Answers 5

82

Try

global $post;
echo $post->ID;

or (I don't know the difference)

global $wp_query;
echo $wp_query->post->ID;
5
  • i have tried all the stuff found on google but nothing happened 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
    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(); Sep 17, 2014 at 14:09
  • first one was correct... Sep 17, 2014 at 14:39
  • 2
    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
    Aug 19, 2015 at 13:59
77

You can simply do,

$page_id = get_queried_object_id();
5
  • 10
    This should be marked as the answer...
    – Lezz
    Jan 28, 2020 at 9:59
  • 7
    Agreed. This should be the answer.
    – CJWEB
    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. May 22, 2020 at 15:38
  • Brilliant, right answer for me Nov 19, 2020 at 22:41
  • Thank you. Great answer. Jun 7, 2021 at 15:05
0

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!

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

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()) {
    the_post();
    $post_id = the_ID();
}
0

Outside the loop:

$current_page = get_page_by_path($_SERVER['REQUEST_URI']);
            
if(isset($current_page->ID)) {
    $current_page_id = $current_page->ID;
}
1
  • 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
    Jun 29 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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