Changeset 946

Show
Ignore:
Timestamp:
02/26/04 21:42:47 (5 years ago)
Author:
emc3
Message:

Added per-post custom metadata support.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/upgrade-functions.php

    r942 r946  
    674674 
    675675function upgrade_110() { 
    676   global $wpdb, $tableusers, $tablecomments, $tableposts, $tableoptiongroups, $tableoptiongroup_options, $tableoptions
     676  global $wpdb, $tableusers, $tablecomments, $tableposts, $tableoptiongroups, $tableoptiongroup_options, $tableoptions, $tablepostmeta
    677677     
    678678    maybe_add_column($tablecomments, 'user_id', "ALTER TABLE `$tablecomments` ADD `user_id` INT DEFAULT '0' NOT NULL ;"); 
     
    754754 
    755755    } 
     756  
     757    // post-meta 
     758    maybe_create_table($tablepostmeta, " 
     759    CREATE TABLE $tablepostmeta ( 
     760      meta_id int(11) NOT NULL auto_increment, 
     761      post_id int(11) NOT NULL default 0, 
     762      meta_key varchar(255), 
     763      meta_value text, 
     764      PRIMARY KEY (meta_id), 
     765      INDEX (post_id), 
     766      INDEX (meta_key) 
     767    ) 
     768    "); 
     769 
    756770} 
    757771 
  • trunk/wp-blog-header.php

    r945 r946  
    449449    } 
    450450 
     451    // Get post-meta info 
     452    if ( $meta_list = $wpdb->get_results(" 
     453            SELECT post_id,meta_key,meta_value  
     454            FROM $tablepostmeta  
     455            WHERE post_id IN($post_id_list) 
     456            ORDER BY post_id,meta_key 
     457        ", ARRAY_A) ) { 
     458         
     459        // Change from flat structure to hierarchical: 
     460        $post_meta_cache = array(); 
     461        foreach ($meta_list as $metarow) { 
     462            $mpid = $metarow['post_id']; 
     463            $mkey = $metarow['meta_key']; 
     464            $mval = $metarow['meta_value']; 
     465             
     466            // Force subkeys to be array type: 
     467            if (!is_array($post_meta_cache[$mpid])) 
     468                $post_meta_cache[$mpid] = array(); 
     469            if (!is_array($post_meta_cache[$mpid][$mkey])) 
     470                $post_meta_cache[$mpid][$mkey] = array(); 
     471             
     472            // Add a value to the current pid/key: 
     473            $post_meta_cache[$mpid][$mkey][] = $mval; 
     474        } 
     475    } 
     476 
     477 
    451478    if (1 == count($posts)) { 
    452479        if ($p || $name) { 
  • trunk/wp-includes/template-functions-post.php

    r945 r946  
    444444} 
    445445 
     446/* 
     447 * Post-meta: Custom per-post fields. 
     448 */ 
     449  
     450function get_post_custom() { 
     451    global $id, $post_meta_cache; 
     452 
     453    return $post_meta_cache[$id]; 
     454} 
     455 
     456function get_post_custom_keys() { 
     457    global $id, $post_meta_cache; 
     458     
     459    if (!is_array($post_meta_cache[$id])) 
     460        return; 
     461    if ($keys = array_keys($post_meta_cache[$id])) 
     462        return $keys; 
     463} 
     464 
     465function get_post_custom_values($key='') { 
     466    global $id, $post_meta_cache; 
     467 
     468    return $post_meta_cache[$id][$key]; 
     469} 
     470 
     471// this will probably change at some point... 
     472function the_meta() { 
     473    global $id, $post_meta_cache; 
     474     
     475    if ($keys = get_post_custom_keys()) { 
     476        echo "<ul>\n"; 
     477        foreach ($keys as $key) { 
     478            $value = implode($post_meta_cache[$id][$key],','); 
     479             
     480            echo "<li>$key: $value</li>\n"; 
     481        } 
     482        echo "</ul>\n"; 
     483    } 
     484} 
     485 
    446486?> 
  • trunk/wp-settings.php

    r945 r946  
    2424$tableoptiongroups        = $table_prefix . 'optiongroups'; 
    2525$tableoptiongroup_options = $table_prefix . 'optiongroup_options'; 
     26$tablepostmeta            = $table_prefix . 'postmeta'; 
    2627define('WPINC', 'wp-includes'); 
    2728require_once (ABSPATH . WPINC . '/wp-db.php');