Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable

In WordPress, each post has unique anchor identifier in the form of numeric post ID. When inside the Loop, where WordPress processes and displays each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags, template tag the_ID can be used to easily display the post’s ID inside the post. Alternatively, inside the the_content filter, declare $id global and use $id to retrieve the post ID.

the_ID template tag can be used in the following format:

<?php the_ID(); ?>

Outside the Loop or out of the context of the post, such as in header of footer and even sidebar area in the templates of WordPress’s blog, the_ID() cannot be used as a function. Instead, $post->ID will be used to return the post ID (the syntax can also be used inside the Loop of the post by declaring $post as global). $post is a global object that holds various information about the posts displayed on the page. So $post->ID will return the post ID of the post. It the $post is used inside a function, the $post has to be declared as a global variable. For example:


// Works inside of the Loop
function function_name() {
global $post;
$thePostID = $post->ID;
}

or:


// Works in single post outside of the Loop
function function_name() {
global $wp_query;
$thePostID = $wp_query->post->ID;
}

$post->ID can be called directly outside of the Loop too in a single post template, such as <?php echo $post->ID ?> will print display the post’s ID number.

In the multiple posts view page such as index page or archive page, it’s possible to retrieve the get the latest or earliest post ID of all the posts displayed on the page by using an ORDER BY clause to order the posts by date, and then set the number of record to LIMIT 1.

Just for example, and there are various way to use the query:

//Get the latest post ID number
$post->ID ORDER BY post_date ASC LIMIT 1

//Get the earliest post ID number
$post->ID ORDER BY post_date DESC LIMIT 1

27 Responses to “Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable”

Pages: 1 2

  1. ilya
    October 19th, 2009 19:51
    25

    thanks. this helped a lot to set up my menu!

  2. clamorbeaver
    October 4th, 2009 00:56
    24

    Thanks a lot, this has solved all of my current problems ;)

  3. Tas
    September 3rd, 2009 23:36
    23

    Many thanks, it worked!

  4. Getting User Permissions to work with YARPP » kimplicity
    August 31st, 2009 05:13
    22

    [...] is not the post being displayed.  The trick was finding the post ID but fortunately this article, Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable, turned out to be immensely [...]

  5. Harpreet Khera
    August 28th, 2009 14:56
    21

    can anybody guide me how to display post tag wise. i have added two tags in posts 1)xxx and 2)www. i want to make two columns one is displayin post with tag xxx and other displaying post with tag www.

  6. Bidyeswar Singh
    August 7th, 2009 13:21
    20

    Hi, I am Testing

  7. Prasanna
    March 2nd, 2009 00:01
    19

    Thanks for the tip.

  8. saliem
    February 26th, 2009 02:03
    18

    thanks :D useful little tips like these are always great :)

  9. clhmedia
    February 13th, 2009 11:59
    17

    awesome, i think this will totally solve a plugin problem with my permalinks…

    i will have to subscribe to this site and see what else i get

    thanks again

  10. Raja
    November 21st, 2008 18:40
    16

    Yeah, Thanks for this post. I solved my permalink problem with this suggestion

  11. dario
    October 13th, 2008 05:36
    15

    Thanks for your great blog. I’ve linked you in the links section of my site.

    I was actually looking for something like what you mention here, but for Blogger.

    What I want is to be able to insert content between posts, but only between Posts 4 thru 9 and not between posts 1 thru 3. Currently I stuck something in the template area but it obviousl appears between every post.

    I’m not a programmer, thought I studies it many years ago.

  12. Boris
    October 9th, 2008 20:56
    14

    Thank you for this.

  13. Daniel
    June 18th, 2008 09:34
    13

    Thanks for this – this did absolutely what I needed to do. :)

  14. PieterC
    June 17th, 2008 14:02
    12

    Thank you. It works like a charm!

  15. Johannes
    June 16th, 2008 03:27
    11

    Forgot Notification of followups. Thanks.

  16. Johannes
    June 16th, 2008 03:27
    10

    Hi,
    thanks for these informations.
    I tried to configure something without any luck and hope you can help me.
    I’m using wordpress as a blog and do have a few pictures in every blog.
    Now I want to implement a feature like shadowbox to popup the images. To do this automatically, I put a rel=”shadowbox[imageset]” in the anchors of the images.
    What do I have to do to put the post-title or ID instead of imageset.
    Is something like this possible?

  17. Shops
    May 6th, 2008 20:02
    9

    Thanks for this. You cant believe how long it took me to search for this simple answer! I dont know why you cant find it on wordpress.

  18. Reviews
    April 18th, 2008 18:11
    8

    Thanks! That’s just what i am looking for. I want to get post id on the wordpress index (root) page.

  19. 让WP不同页面使用不同设计 | 收拾起大地山河一担装
    October 10th, 2007 21:05
    7

    [...] 使用post ID变量可以做到。$post->ID可以返回文章的唯一编号。 [...]

  20. Osordvibil
    October 6th, 2007 00:09
    6

    comment_fashion

  21. Eric
    September 21st, 2007 07:51
    5

    Thanks, using ‘global $id;’ in a filter for my plugin got me the post id I needed.

  22. k
    June 28th, 2007 11:55
    4

    Great info. Needed this a few days ago. Will update my code with this direct approach. Thanks.

  23. Christophe
    May 16th, 2007 14:15
    3

    Found this through Google. Thanks! You should also enter this into the official WP documentation wiki: http://codex.wordpress.org/Main_Page

  24. Matt
    May 3rd, 2007 05:10
    2

    Just what I was looking for, thanks!

  25. Joel P
    December 1st, 2006 06:06
    1

    This is really good thing to know… I think i’ll try to use this with my blog and limited php knowledge.

Pages: 1 2

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.

New Articles

Incoming Search Terms for the Article

wordpress get post id - wordpress post id - wordpress get page id - get post id wordpress - wordpress get current page id - $post->ID - post ID wordpress - wordpress get current post id - get post id - wordpress $post->ID - wordpress get id - wordpress current post id - wordpress get post id outside loop - get page id wordpress - wordpress current page id - wordpress post id variable - post id - wordpress id - wordpress return post id - wp get post id - get wordpress post id - get post id outside loop - wordpress page id - wordpress echo post id - how to get page id in wordpress - post->id - wordpress get page by id - how to get post id in wordpress - $post->id wordpress - wordpress get the id - wp post id - wp get page id - get current post id wordpress - wordpress get_ID - wordpress php post id - wordpress get current post - wordpress post_id - wordpress get pageid - wordpress post id outside loop - wordpress post id outside the loop - get post id in wordpress - wordpress post by id - wordpress the id - wordpress get current page - get the post id wordpress - how to get post id wordpress - wordpress page id variable - get current page id wordpress - post id in wordpress - wordpress get the post id - wordpress get post by id - wordpress get page id outside loop - wordpress plugin get post id - wordpress get post id - wordpress postid - wordpress outside loop - post_id wordpress - get post id outside the loop - wordpress current post - wordpress get post - wp current page id - get page id in wordpress - wordpress order by post id - how to get page id wordpress outside loop - display page ID wordpress - post- id wordpress - wordpress post object - get wordpress page id - wordpress get post category - wordpress display page ID - get id+wordpress - wordpress get page id php - wordpress the post id - wordpress how to get post id - wordpress post id outside of loop -