Added per-post custom metadata support.

git-svn-id: http://svn.automattic.com/wordpress/trunk@946 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
emc3 2004-02-26 21:42:47 +00:00
parent 8927968849
commit 6df1e26f2c
4 changed files with 85 additions and 3 deletions

View File

@ -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)
)
");
}
?>

View File

@ -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;

View File

@ -443,4 +443,44 @@ function posts_nav_link($sep=' :: ', $prelabel='<< Previous Page', $nxtlabel='Ne
}
}
/*
* 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 "<ul>\n";
foreach ($keys as $key) {
$value = implode($post_meta_cache[$id][$key],',');
echo "<li>$key: $value</li>\n";
}
echo "</ul>\n";
}
}
?>

View File

@ -23,6 +23,7 @@ $tableoptiontypes = $table_prefix . 'optiontypes';
$tableoptionvalues = $table_prefix . 'optionvalues';
$tableoptiongroups = $table_prefix . 'optiongroups';
$tableoptiongroup_options = $table_prefix . 'optiongroup_options';
$tablepostmeta = $table_prefix . 'postmeta';
define('WPINC', 'wp-includes');
require_once (ABSPATH . WPINC . '/wp-db.php');