WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Dealing with retrieving CPT metadata I find out two methods get_post_meta() and get_post_custom() to return the value(s) of the custom fields with the specified key from the specified post. Both methods are passing the $post_id as first parameter to access global $post object's ID property.

Now my question is, how does WordPress figure out which CPT or POST we are asking for? For example at following example how does WordPress understand which post we are talking about

function save_options(){
  global $post;
  if (!isset($_POST['price']) || $post->post_type != 'product')
    return $post;
  update_post_meta($post->ID, "price", $_POST['price']);
share|improve this question
I am not following your question. If method accepts unique numerical ID for a post, that is precisely how it knows that is post to work with. – Rarst May 26 '13 at 20:24
up vote 2 down vote accepted

I consider this question to be borderline unanswerable. It really depends on context.

In your example $post is whatever that global has been set to. That is pretty general, pretty obvious, and not really saying much but without knowing the context in which this code executes it is hard to give a better answer.

That variable is set for each post in the Loop by setup_postdata or the_post and it is set on some back-end pages in similar ways, based ultimately in $_GET or $_POST data whether that $_GET or $_POST data contains a post ID or components of a query.

Many of the hooks and filters that you use are passed that value so it is not always necessary to grab the global directly.

share|improve this answer
Thanks s_sh_dum – Behseini May 27 '13 at 16:22

Your Answer


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.