0

I don't understand when to use $post->ID or $post->post->ID with CPTs. I have :

$event = get_post( $event_id );
$event_pre_register = get_post_meta($event->post->ID, "Event Pre-register deadline", true); 

Or is it : get_post_meta($event->ID ... ?

2 Answers 2

1

If you have the CPT's ID in the $event_id variable you plug into the get_post() call - why would you not simply just (re)use that for the first parameter of get_post_meta() ?

That aside, get_post() returns a post object (instance of WP_Post), which you choose to assign to $event. Hence $event now has a public property $ID, which can be accessed by $event->ID.

2

You can just reuse $event_id here. $event->ID should be fine too.

Inspect your variables with a little helper like this:

print '<pre>$event = '
    . htmlspecialchars( print_r( $event, TRUE ), ENT_QUOTES, 'utf-8', FALSE )
    . "</pre>\n";
1
  • +1 for the inspection snippet. Simple, but anyhoo, I usually dump shit with a straight var_dump - fine for a single float or the like, but this makes large objects or nested associative arrays so much more readable. Man, I dig "stealing" your code. ;) Apr 5, 2013 at 4:28

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.