Make WordPress Core

Changeset 3486


Ignore:
Timestamp:
01/25/2006 07:38:43 AM (21 years ago)
Author:
ryan
Message:

post_custom cleanup. fixes #2160

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/template-functions-post.php

    r3448 r3486  
    194194function get_post_custom( $post_id = 0 ) {
    195195        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 
    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();
    214 
    215                                 // Add a value to the current pid/key:
    216                                 $post_meta_cache[$mpid][$mkey][] = $mval;
    217                         }
     196
     197        if ( ! $post_id )
     198                $post_id = $id;
     199
     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                }
    218221                return $post_meta_cache[$mpid];
    219                 }
    220222        }
    221223}
    … …  
    223225
    224226function get_post_custom_keys() {
    225         global $id, $post_meta_cache;
    226 
    227         if ( !is_array($post_meta_cache[$id]) )
     227        $custom = get_post_custom();
     228
     229        if ( ! is_array($custom) )
    228230                return;
    229         if ( $keys = array_keys($post_meta_cache[$id]) )
     231
     232        if ( $keys = array_keys($custom) )
    230233                return $keys;
    231234}
    … …  
    233236
    234237function get_post_custom_values( $key = '' ) {
    235         global $id, $post_meta_cache;
    236 
    237         if ( empty($key) )
    238                 return $post_meta_cache[$id];
     238        $custom = get_post_custom();
     239
     240        return $custom[$key];
     241}
     242
     243
     244function post_custom( $key = '' ) {
     245        $custom = get_post_custom();
     246
     247        if ( 1 == count($custom[$key]) )
     248                return $custom[$key][0];
    239249        else
    240                 return get_post_custom();
    241 }
    242 
    243 
    244 function post_custom( $key = '' ) {
    245         global $id, $post_meta_cache;
    246 
    247         if ( 1 == count($post_meta_cache[$id][$key]) )
    248                 return $post_meta_cache[$id][$key][0];
    249         else
    250                 return $post_meta_cache[$id][$key];
     250                return $custom[$key];
    251251}
    252252
    … …  
    259259                echo "<ul class='post-meta'>\n";
    260260                foreach ( $keys as $key ) {
    261                         $values = array_map('trim',$post_meta_cache[$id][$key]);
     261                        $values = array_map('trim', get_post_custom_values($key));
    262262                        $value = implode($values,', ');
    263263                        echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n";
Note: See TracChangeset for help on using the changeset viewer.