How to get meta key list efficiently?

I have this function to get the meta keys for all the post types.

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

Though it works fine it results in high database queries and duplicates queries. Is there any method to improve this or alternative solutions?

Read more here:: How to get meta key list efficiently?

Leave a Reply

Your email address will not be published. Required fields are marked *