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
Share and contribute or get technical support and help at My Digital Life Forums.
Related Articles
- Paginating or Split WordPress Post or Page with NextPage in WordPress Not Working
- Disable and Turn Off Post Revisions Tracking in WordPress 2.6 or Above
- How to Change the Frequency or Interval WordPress Auto Saves An Editing Post or Page
- WordPress MySQL SQL Query Error in WPDB Class
- Adding Extra More Fields to WordPress Write/Edit Post/Page Right Column
- Disable Auto Go To (Jump) To Read More Tag
- How To Store Every Post Revisions on Each Auto-Save in WordPress with Limit on Versions Count
- 3 Column Relaxation WordPress Theme comments-paged.php for Paged-Comments WordPress Plugin
- How to Delete Existing WordPress Post Revisions Stored/Saved
- Integrate WordPress including Comments with bbPress Forum using bbSync

































December 1st, 2006 06:06
This is really good thing to know… I think i’ll try to use this with my blog and limited php knowledge.
May 3rd, 2007 05:10
Just what I was looking for, thanks!
May 16th, 2007 14:15
Found this through Google. Thanks! You should also enter this into the official WP documentation wiki: http://codex.wordpress.org/Main_Page
June 28th, 2007 11:55
Great info. Needed this a few days ago. Will update my code with this direct approach. Thanks.
September 21st, 2007 07:51
Thanks, using ‘global $id;’ in a filter for my plugin got me the post id I needed.
October 6th, 2007 00:09
comment_fashion
October 10th, 2007 21:05
[...] 使用post ID变量可以做到。$post->ID可以返回文章的唯一编号。 [...]
April 18th, 2008 18:11
Thanks! That’s just what i am looking for. I want to get post id on the wordpress index (root) page.
May 6th, 2008 20:02
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.
June 16th, 2008 03:27
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?
June 16th, 2008 03:27
Forgot Notification of followups. Thanks.
June 17th, 2008 14:02
Thank you. It works like a charm!
June 18th, 2008 09:34
Thanks for this - this did absolutely what I needed to do.
September 7th, 2008 14:58
it helped me to fixing up my site. thanks