50

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

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 '14 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 '14 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 '14 at 14:09
  • first one was correct... Sep 17 '14 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 '15 at 13:59
67

You can simply do,

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

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.