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. Konisto
    November 7th, 2009 03:41
    27

    Hi,

    I’m trying to modify a custom post-new.php in the admin area and need to access the post_id of a post that I’m currently editing and that has not yet been published. Any idea how I could do that?

    Thanks

  2. Blake
    October 30th, 2009 02:22
    26

    Thanks, this was super-helpful and easy to understand! I love code samples!

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 -