post_custom cleanup. fixes #2160

git-svn-id: https://develop.svn.wordpress.org/trunk@3486 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2006-01-25 07:38:43 +00:00
parent 97c6c5ee4f
commit c9096b231b

View File

@ -193,12 +193,14 @@ Post-meta: Custom per-post fields.
function get_post_custom( $post_id = 0 ) { function get_post_custom( $post_id = 0 ) {
global $id, $post_meta_cache, $wpdb; global $id, $post_meta_cache, $wpdb;
if ( $post_id )
$id = $post_id; if ( ! $post_id )
if ( isset($post_meta_cache[$id]) ) { $post_id = $id;
return $post_meta_cache[$id];
} else { if ( isset($post_meta_cache[$post_id]) )
if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = '$id' ORDER BY post_id, meta_key", ARRAY_A) ) { return $post_meta_cache[$post_id];
if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' ORDER BY post_id, meta_key", ARRAY_A) ) {
// Change from flat structure to hierarchical: // Change from flat structure to hierarchical:
$post_meta_cache = array(); $post_meta_cache = array();
foreach ( $meta_list as $metarow ) { foreach ( $meta_list as $metarow ) {
@ -209,6 +211,7 @@ function get_post_custom( $post_id = 0 ) {
// Force subkeys to be array type: // Force subkeys to be array type:
if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) ) if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) )
$post_meta_cache[$mpid] = array(); $post_meta_cache[$mpid] = array();
if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) ) if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) )
$post_meta_cache[$mpid]["$mkey"] = array(); $post_meta_cache[$mpid]["$mkey"] = array();
@ -218,36 +221,33 @@ function get_post_custom( $post_id = 0 ) {
return $post_meta_cache[$mpid]; return $post_meta_cache[$mpid];
} }
} }
}
function get_post_custom_keys() { function get_post_custom_keys() {
global $id, $post_meta_cache; $custom = get_post_custom();
if ( !is_array($post_meta_cache[$id]) ) if ( ! is_array($custom) )
return; return;
if ( $keys = array_keys($post_meta_cache[$id]) )
if ( $keys = array_keys($custom) )
return $keys; return $keys;
} }
function get_post_custom_values( $key = '' ) { function get_post_custom_values( $key = '' ) {
global $id, $post_meta_cache; $custom = get_post_custom();
if ( empty($key) ) return $custom[$key];
return $post_meta_cache[$id];
else
return get_post_custom();
} }
function post_custom( $key = '' ) { function post_custom( $key = '' ) {
global $id, $post_meta_cache; $custom = get_post_custom();
if ( 1 == count($post_meta_cache[$id][$key]) ) if ( 1 == count($custom[$key]) )
return $post_meta_cache[$id][$key][0]; return $custom[$key][0];
else else
return $post_meta_cache[$id][$key]; return $custom[$key];
} }
@ -258,7 +258,7 @@ function the_meta() {
if ( $keys = get_post_custom_keys() ) { if ( $keys = get_post_custom_keys() ) {
echo "<ul class='post-meta'>\n"; echo "<ul class='post-meta'>\n";
foreach ( $keys as $key ) { foreach ( $keys as $key ) {
$values = array_map('trim',$post_meta_cache[$id][$key]); $values = array_map('trim', get_post_custom_values($key));
$value = implode($values,', '); $value = implode($values,', ');
echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n"; echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n";
} }