Created
August 16, 2013 20:52
-
-
Save tybruffy/6253428 to your computer and use it in GitHub Desktop.
Get all distinct values of a meta field in Wordpress
This file contains hidden or 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; | |
} |
Thanks a lot! It was extremely helpful!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just great! Nice Work! Thank you!