Created
August 7, 2014 07:49
-
-
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
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
<?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