-
-
Save bilalmalkoc/30d41473118ba69033c641e21853bfff to your computer and use it in GitHub Desktop.
Get all distinct values of a meta field in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _get_all_meta_values($key) { | |
global $wpdb; | |
$result = $wpdb->get_col( | |
$wpdb->prepare( " | |
SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm | |
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id | |
WHERE pm.meta_key = '%s' | |
AND p.post_status = 'publish' | |
ORDER BY pm.meta_value", | |
$key | |
) | |
); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment