Skip to content

Instantly share code, notes, and snippets.

@tybruffy
Created August 16, 2013 20:52
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tybruffy/6253428 to your computer and use it in GitHub Desktop.
Save tybruffy/6253428 to your computer and use it in GitHub Desktop.
Get all distinct values of a meta field in Wordpress
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;
}
@mariusrazoux
Copy link

mariusrazoux commented Jun 28, 2019

thank you, very useful

@mtachaudhary
Copy link

Thank you so much, helpful for me

@orfibous
Copy link

This is just great! Nice Work! Thank you!

@EmmanouilLysikatos
Copy link

EmmanouilLysikatos commented Mar 18, 2022

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