diff --git a/wp-admin/upgrade-functions.php b/wp-admin/upgrade-functions.php index ecea10a892..0b96f8bb10 100644 --- a/wp-admin/upgrade-functions.php +++ b/wp-admin/upgrade-functions.php @@ -673,7 +673,7 @@ function upgrade_101() { } function upgrade_110() { - global $wpdb, $tableusers, $tablecomments, $tableposts, $tableoptiongroups, $tableoptiongroup_options, $tableoptions; + global $wpdb, $tableusers, $tablecomments, $tableposts, $tableoptiongroups, $tableoptiongroup_options, $tableoptions, $tablepostmeta; maybe_add_column($tablecomments, 'user_id', "ALTER TABLE `$tablecomments` ADD `user_id` INT DEFAULT '0' NOT NULL ;"); maybe_add_column($tableusers, 'user_activation_key', "ALTER TABLE `$tableusers` ADD `user_activation_key` VARCHAR( 60 ) NOT NULL ;"); @@ -753,6 +753,20 @@ function upgrade_110() { } } + + // post-meta + maybe_create_table($tablepostmeta, " + CREATE TABLE $tablepostmeta ( + meta_id int(11) NOT NULL auto_increment, + post_id int(11) NOT NULL default 0, + meta_key varchar(255), + meta_value text, + PRIMARY KEY (meta_id), + INDEX (post_id), + INDEX (meta_key) + ) + "); + } ?> \ No newline at end of file diff --git a/wp-blog-header.php b/wp-blog-header.php index 436f46873b..cc4aec4e7c 100644 --- a/wp-blog-header.php +++ b/wp-blog-header.php @@ -448,6 +448,33 @@ if ($posts) { $comment_count_cache["$comment_count->ID"] = $comment_count->ccount; } + // Get post-meta info + if ( $meta_list = $wpdb->get_results(" + SELECT post_id,meta_key,meta_value + FROM $tablepostmeta + WHERE post_id IN($post_id_list) + ORDER BY post_id,meta_key + ", ARRAY_A) ) { + + // Change from flat structure to hierarchical: + $post_meta_cache = array(); + foreach ($meta_list as $metarow) { + $mpid = $metarow['post_id']; + $mkey = $metarow['meta_key']; + $mval = $metarow['meta_value']; + + // Force subkeys to be array type: + if (!is_array($post_meta_cache[$mpid])) + $post_meta_cache[$mpid] = array(); + if (!is_array($post_meta_cache[$mpid][$mkey])) + $post_meta_cache[$mpid][$mkey] = array(); + + // Add a value to the current pid/key: + $post_meta_cache[$mpid][$mkey][] = $mval; + } + } + + if (1 == count($posts)) { if ($p || $name) { $more = 1; diff --git a/wp-includes/template-functions-post.php b/wp-includes/template-functions-post.php index eb117af566..0541881647 100644 --- a/wp-includes/template-functions-post.php +++ b/wp-includes/template-functions-post.php @@ -443,4 +443,44 @@ function posts_nav_link($sep=' :: ', $prelabel='<< Previous Page', $nxtlabel='Ne } } -?> \ No newline at end of file +/* + * Post-meta: Custom per-post fields. + */ + +function get_post_custom() { + global $id, $post_meta_cache; + + return $post_meta_cache[$id]; +} + +function get_post_custom_keys() { + global $id, $post_meta_cache; + + if (!is_array($post_meta_cache[$id])) + return; + if ($keys = array_keys($post_meta_cache[$id])) + return $keys; +} + +function get_post_custom_values($key='') { + global $id, $post_meta_cache; + + return $post_meta_cache[$id][$key]; +} + +// this will probably change at some point... +function the_meta() { + global $id, $post_meta_cache; + + if ($keys = get_post_custom_keys()) { + echo "