Sometime we need to find all user created custom field key names (or meta key) in wordpress. This may be needed to find all posts and pages having a certain custom field key and value for audit/review purpose. Here is code which can be used for this:
global $wpdb;
$querystr = "
SELECT distinct $wpdb->postmeta.meta_key
FROM $wpdb->postmeta
WHERE $wpdb->postmeta.meta_key NOT BETWEEN '_' AND '_z'
ORDER BY $wpdb->postmeta.meta_key
";
$res_list = $wpdb->get_results($querystr);
if ($res_list) {
echo "custom keys:\n";
foreach ($res_list as $obj) {
$key = $obj->meta_key;
echo "$key\n";
}
}