我有这个函数来获取所有帖子类型的元键。
public function pgggo_custom_post_type_keylist()
{
//gets the list of post types
$pgggo_custompostype_array = $this->pgggo_list_of_posttypes();
$pgggo_getposts = array();
foreach ($pgggo_custompostype_array as $value) {
$args = array(
\'numberposts\' => 1,
\'post_type\' => $value,
);
if (!empty(get_posts($args)[0]->ID)) {
$pgggo_getposts[] = get_posts($args)[0]->ID;
}
}
$new_array = array();
foreach ($pgggo_getposts as $value) {
$new_array[] = get_post_custom($value);
}
$result = array();
foreach ($new_array as $sub) {
$result = array_merge($result, $sub);
}
$result = array_keys($result);
$result = array_unique($result);
$result = array_combine($result, $result);
$result[\'\'] = \'NONE\';
return $result;
}
虽然它工作得很好,但会导致高数据库查询和重复查询。是否有任何方法可以改进此解决方案或替代解决方案?
最合适的回答,由SO网友:freejack 整理而成
如注释中所述,您可以直接查询Posteta表:
public function get_metadata_keys(){
global $wpdb;
$meta_query = $wpdb->get_results(
"
SELECT DISTINCT meta_key
FROM {$wpdb->postmeta}
"
, ARRAY_A );
$meta_keys = wp_list_pluck( $meta_query, \'meta_key\' );
return $meta_keys;
}
这将返回与任何帖子关联的所有meta\\u键的列表。