Make WordPress Core

Ticket #2160: meta.diff

File meta.diff, 3.4 KB (added by ryan, 18 years ago)

post custom cleanup

  • wp-includes/template-functions-post.php

     
    193193
    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'];
    208196
    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;
    214199
    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                }
    218221                return $post_meta_cache[$mpid];
    219                 }
    220222        }
    221223}
    222224
    223225
    224226function get_post_custom_keys() {
    225         global $id, $post_meta_cache;
     227        $custom = get_post_custom();
    226228
    227         if ( !is_array($post_meta_cache[$id]) )
     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}
    232235
    233236
    234237function get_post_custom_values( $key = '' ) {
    235         global $id, $post_meta_cache;
     238        $custom = get_post_custom();
    236239
    237         if ( empty($key) )
    238                 return $post_meta_cache[$id];
    239         else
    240                 return get_post_custom();
     240        return $custom[$key];
    241241}
    242242
    243243
    244244function post_custom( $key = '' ) {
    245         global $id, $post_meta_cache;
     245        $custom = get_post_custom();
    246246
    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];
    249249        else
    250                 return $post_meta_cache[$id][$key];
     250                return $custom[$key];
    251251}
    252252
    253253
     
    258258        if ( $keys = get_post_custom_keys() ) {
    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";
    264264                }