Wordpress通过metabox ID获取自定义字段
问题描述:
我在我的写作文章面板中注册了多个metabox。在functions.php中:Wordpress通过metabox ID获取自定义字段
$meta_boxes[] = array(
'id' => 'products',
'title' => 'Products',
'pages' => array('post', 'page', 'link'), // multiple post types, accept custom post types
'context' => 'normal', // normal, advanced, side (optional)
'priority' => 'high', // high, low (optional)
'fields' => array(
array(
'name' => 'something',
'id' => $prefix . 'something',
'desc' => 'some description',
'type' => 'checkbox'
),
//continue with other fields
)
);
//another metabox
$meta_boxes[] = array(
'id' => 'customers',
'title' => 'Customers',
//etc...
我能够用得到在后场的所有值:
$custom_field_keys = get_post_custom_keys();
foreach ($custom_field_keys as $key => $value) {
$valuet = trim($value);
if ('_' == $valuet{0})
continue;
echo $value . ":<br />";
echo get_post_meta($post->ID, $value, true) . "<br/><br/>";
}
我怎样才能得到一个metabox内的所有字段的名称和值我指定。例如,获取metabox id“products”中的所有字段值。
答
我的建议是看看这是如何表示在数据库(可能在postmeta表),并使用$ wpdb - wordpress数据库连接类来查询数据库使用后缀ID和字段名称的元框名称 - 实际上有人已经这样做了,作为meta-box的扩展 - 在meta-box网站中看到了一个参考。
谢谢!我查看了数据库表,并且没有包含元组名称的列。也许我必须添加自己的列来实现这个可能 – CyberJunkie
在wordpress插件开发中已知的做法是将这样的数据存储在选项表或其他地方的数组中,可能不是单独的列。但是请放心,它在数据库中是有的......或者它不存在..我确信它并不是硬编码的插件。所以查询/搜索整个wordpress数据库,你会找到答案 – alonisser