Skip to content

Instantly share code, notes, and snippets.

@voodooGQ
Last active December 26, 2015 22:29
Show Gist options
  • Save voodooGQ/7223008 to your computer and use it in GitHub Desktop.
Save voodooGQ/7223008 to your computer and use it in GitHub Desktop.
Post Meta Object
<?php
/**
* Returns all "public" meta items for $post in the form of an object
* Useful for reducing server calls
*
* @param int $post_id The ID of the post
* @return object
*/
function post_meta_object($post_id) {
$meta = get_post_meta($post_id);
$object = new stdClass();
foreach($meta as $key => $value) {
if(substr($key, 0, 1) === "_") {
continue;
}
if(is_array($value)) {
$value = $value[0];
}
$object->$key = $value;
}
return $object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment