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've seen some examples that use $_post instead of $post. What's the difference and when would you use $_post instead of $post?

Example:

if (have_posts()) :
while (have_posts()) : the_post();
if(!in_category("some-category", $_post )) { do_something(); }
share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

I'm not certain, but if you're talking about $_post as seen here:

http://codex.wordpress.org/Function_Reference/in_category

It's referring to a post ID so they've probably added the underscore to avoid confusion. The $post variable is a post object with all the details of the post.

However, $_POST is a global PHP variable containing all the values sent from a form:

http://php.net/manual/en/reserved.variables.post.php

share|improve this answer
    
Exactly, now I remember where I got it from. Its puzzled me since. Thanks for clearing that up :) –  Scott B Feb 6 '11 at 15:51
add comment

Q: When would you use $_post instead of $post?

Whenever I think it's applicable, for your example I have no idea.

Q: What's the difference and when would you use $_post instead of $post?

The difference is that you are using different variables.

Different variables

This is more or less a coding question, but I try to be as helpful as I can to answer it in the context of WordPress.

$_post is just a variable, the $ identifies that it's a variable and _post is the variable's name. Those names are case-sensitive btw., so $_post and $_Post or $_POST are all different variables. And $post for sure is as well.

If you want to learn what's in a variable - ie to find out more about it - you can make use of the var_dump() function: var_dump($_post);.

This can be helpful to find out more, because with the code you provided it can not be said for what the $_post variable stands for.

As variables can contain any value (incl. being even not set) you need to find the part of the code that is setting the variable.

Special Meaning of $post in the loop

The variable $post has a special meaning in the context of the_loop: It contains a reference to the current post in the loop.

share|improve this answer
1  
+1 thanks for taking the time to explain this. As Pabline noted, my reference came from the codex example for in_category, but your explanation helps paint the full picture. –  Scott B Feb 6 '11 at 15:53
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.