I've seen some examples that use $_post instead of $post. What's the difference and when would you use $_post instead of $post?


if (have_posts()) :
while (have_posts()) : the_post();
if(!in_category("some-category", $_post )) { do_something(); }

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


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:


  • 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

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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