Skip to content

Instantly share code, notes, and snippets.

@nciske
Created September 18, 2012 17:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nciske/3744542 to your computer and use it in GitHub Desktop.
Save nciske/3744542 to your computer and use it in GitHub Desktop.
WordPress Getting all values for a custom field key
// http://wordpress.stackexchange.com/questions/9394/getting-all-values-for-a-custom-field-key-cross-post
function get_meta_values( $key = '', $type = 'post', $status = 'publish' ) {
global $wpdb;
if( empty( $key ) )
return;
$r = $wpdb->get_col( $wpdb->prepare( "
SELECT 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 = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
return $r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment