Skip to content

Instantly share code, notes, and snippets.

@cpaul007
Created August 7, 2014 07:49
Show Gist options
  • Save cpaul007/c63aa65baba9ae17b3d4 to your computer and use it in GitHub Desktop.
Save cpaul007/c63aa65baba9ae17b3d4 to your computer and use it in GitHub Desktop.
Getting all the values associated with a specific custom post meta key, across all posts
<?php
/* Don't include the opening PHP tag*/
/**
* Description: Getting all the values associated with a specific custom post meta key, across all posts
* Author: Chinmoy Paul
* Author URL: http://pwdtechnology.com
*
* @param string $key Post Meta Key.
*
* @param string $type Post Type. Default is post. You can pass custom post type here.
*
* @param string $status Post Status like Publish, draft, future etc. default is publish
*
* @return array
*/
function get_unique_post_meta_values( $key = '', $type = 'post', $status = 'publish' ) {
global $wpdb;
if( empty( $key ) )
return;
$res = $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 = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment