wordpress数据库中存储有一个postmeta的数据表,这个表的作用是当posts表中的字段不够使用时,可以向postmeta表中存储额外的数据。这是一个非常灵活的数据结构,有利于wordpress插件编写者方便的扩展数据,而不需要更改数据表结构,有利于代码升级。非常赞!
postmeta表中可以针对一篇post或者page创建多个参数,而且每个参数都可以存储数组数据,为啥能存储数组数据呢?因为 wordpress对存储的数据采取了json格式编码,json是一个轻量级的数据结构,可以存储数组结构,它来源于javascript。如果存储数组数据在postmeta中,wordpress会在存入和读出时自动对数据进行编码和解码。下面是add_post_meta和 update_post_meta两个函数的使用说明:
* @param int $post_id post ID
* @param string $key {@internal Missing Description}}
* @param mixed $value {@internal Missing Description}}
* @param bool $unique whether to check for a value with the same key
* @return bool {@internal Missing Description}}
add_post_meta($post_id, $meta_key, $meta_value, $unique = false)
* @param int $post_id post ID
* @param string $key {@internal Missing Description}}
* @param mixed $value {@internal Missing Description}}
* @param mixed $prev_value previous value (for differentiating between meta fields with the same key and post ID)
* @return bool {@internal Missing Description}}
update_post_meta($post_id, $meta_key, $meta_value, $prev_value = ”)
这里需要说明的一点是:如果存储数组数据,数组必须是字符串索引,否则会出现奇怪的现象!

Post a Comment