I'm trying to attach an object to every post of any type. This will require post ID to populate or create my object. So, I either call global $post, or get_the_ID(), or do get_post(). I hope there is a simple way to do the job, and I found a plugin "wp-post-ratings" uses global $id, it gets the id within the loop. I wonder if this is a WP global, or it's the plugin's custom code?

4 Answers 4


If you are using netbeans and xdebug, or have set up your debugging environment correctly then you can observe the variables, objects e.t.c as they change.

To see when they are created and what is contained within objects such as wp_query or wp_rewrite your posts->ID is set once the function wp() is called in wp-blog-header.php.

I know you asked where the ID is set but you can use this to find other variables and objects that are set by stepping through the wordpress environment. That is if you develop your wordpress sites locally.

Netbeans Variables

  • Thanks for your suggestion! I will try. I haven't ever figured out how to use debug tools in any IDE. Every time I click run or debug, it evaluate the entire wp project, that takes a long time.
    – Jenny
    Aug 6, 2012 at 13:29
  • hey no worries, it only takes six button presses to view all variables after wp() is called =) F7 to go into require('./wp-blog-header.php'); -> then F8 to go past wp() and the variables will load, I would definitely recommend getting comfortable and practicing if you are going to use wordpress regularly
    – jhussey
    Aug 6, 2012 at 13:37

You can retrieve an ID from the $post global using the method $post->ID. This will retrieve the ID from the last $post object, so if you or something else (a plugin or whatever) modifies the main query_posts() before you're attempting to get your ID, then the ID will be different.

I would say if you want to ensure you're getting the actual page/post/item id then to access $wp_query which is a class that WordPress uses prior to all the other loops.

global $wp_query;
$thePostID = $wp_query->post->ID;
  • Great to know there is a hint of sequence. If I want a custom post under buddypress Group page. This $wp_query->post-ID will provide the group page ID or the post ID?
    – Jenny
    Aug 6, 2012 at 12:50
  • I just tried, $wp_query->post->ID; is the most accurate,but still can get wrong id sometimes.
    – Jenny
    Aug 6, 2012 at 13:25

$id is one of the global variable available only from inside the loop. If you want to modify content of every post, wordpress has a hook for that called the_content

  • yes, I use the_content to show the object, or place code within the loop. but when create or edit, it is a problem to find correct id
    – Jenny
    Aug 6, 2012 at 12:52

Yup, It is a global variable but works only inside Wordpress loop.

While inside the loop, $id (int) global is set, containing ID of the current post being processed.

Reference - Codex - Inside the Loop variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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