Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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

share|improve this answer
    
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 '12 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 –  thirdOctet Aug 6 '12 at 13:37
add comment

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;
share|improve this answer
    
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 '12 at 12:50
    
I just tried, $wp_query->post->ID; is the most accurate,but still can get wrong id sometimes. –  Jenny Aug 6 '12 at 13:25
add comment

$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

share|improve this answer
    
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 '12 at 12:52
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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