Ticket #2160: meta.diff
File meta.diff, 3.4 KB (added by , 18 years ago) |
---|
-
wp-includes/template-functions-post.php
193 193 194 194 function get_post_custom( $post_id = 0 ) { 195 195 global $id, $post_meta_cache, $wpdb; 196 if ( $post_id )197 $id = $post_id;198 if ( isset($post_meta_cache[$id]) ) {199 return $post_meta_cache[$id];200 } else {201 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) ) {202 // Change from flat structure to hierarchical:203 $post_meta_cache = array();204 foreach ( $meta_list as $metarow ) {205 $mpid = $metarow['post_id'];206 $mkey = $metarow['meta_key'];207 $mval = $metarow['meta_value'];208 196 209 // Force subkeys to be array type: 210 if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) ) 211 $post_meta_cache[$mpid] = array(); 212 if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) ) 213 $post_meta_cache[$mpid]["$mkey"] = array(); 197 if ( ! $post_id ) 198 $post_id = $id; 214 199 215 // Add a value to the current pid/key: 216 $post_meta_cache[$mpid][$mkey][] = $mval; 217 } 200 if ( isset($post_meta_cache[$post_id]) ) 201 return $post_meta_cache[$post_id]; 202 203 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) ) { 204 // Change from flat structure to hierarchical: 205 $post_meta_cache = array(); 206 foreach ( $meta_list as $metarow ) { 207 $mpid = $metarow['post_id']; 208 $mkey = $metarow['meta_key']; 209 $mval = $metarow['meta_value']; 210 211 // Force subkeys to be array type: 212 if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) ) 213 $post_meta_cache[$mpid] = array(); 214 215 if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) ) 216 $post_meta_cache[$mpid]["$mkey"] = array(); 217 218 // Add a value to the current pid/key: 219 $post_meta_cache[$mpid][$mkey][] = $mval; 220 } 218 221 return $post_meta_cache[$mpid]; 219 }220 222 } 221 223 } 222 224 223 225 224 226 function get_post_custom_keys() { 225 global $id, $post_meta_cache;227 $custom = get_post_custom(); 226 228 227 if ( ! is_array($post_meta_cache[$id]) )229 if ( ! is_array($custom) ) 228 230 return; 229 if ( $keys = array_keys($post_meta_cache[$id]) ) 231 232 if ( $keys = array_keys($custom) ) 230 233 return $keys; 231 234 } 232 235 233 236 234 237 function get_post_custom_values( $key = '' ) { 235 global $id, $post_meta_cache;238 $custom = get_post_custom(); 236 239 237 if ( empty($key) ) 238 return $post_meta_cache[$id]; 239 else 240 return get_post_custom(); 240 return $custom[$key]; 241 241 } 242 242 243 243 244 244 function post_custom( $key = '' ) { 245 global $id, $post_meta_cache;245 $custom = get_post_custom(); 246 246 247 if ( 1 == count($ post_meta_cache[$id][$key]) )248 return $ post_meta_cache[$id][$key][0];247 if ( 1 == count($custom[$key]) ) 248 return $custom[$key][0]; 249 249 else 250 return $ post_meta_cache[$id][$key];250 return $custom[$key]; 251 251 } 252 252 253 253 … … 258 258 if ( $keys = get_post_custom_keys() ) { 259 259 echo "<ul class='post-meta'>\n"; 260 260 foreach ( $keys as $key ) { 261 $values = array_map('trim', $post_meta_cache[$id][$key]);261 $values = array_map('trim', get_post_custom_values($key)); 262 262 $value = implode($values,', '); 263 263 echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n"; 264 264 }