Skip to content

Instantly share code, notes, and snippets.

@jorijn
Created December 27, 2012 08:03
Show Gist options
  • Save jorijn/4386456 to your computer and use it in GitHub Desktop.
Save jorijn/4386456 to your computer and use it in GitHub Desktop.
get_unique_meta
/**
* returns an array of unique items of meta_value, this was originally
* built for a filter machanism.
*
* @param string $post_type example: post
* @param string $meta_key example: _wp_page_template
* @return array
*/
function get_unique_meta($post_type, $meta_key)
{
global $wpdb;
$prepare = $wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta m INNER JOIN $wpdb->posts p ON (m.post_id = p.ID) WHERE p.post_type = %s AND meta_key = %s ORDER BY meta_value ASC", $post_type, $meta_key);
$result = $wpdb->get_results($prepare);
$return = array();
foreach ((array)$result as $item)
{
$return[] = $item->meta_value;
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment