Monday, 26 August 2013

How to Modify this $wpdb query to accept an array of post statuses

How to Modify this $wpdb query to accept an array of post statuses

I'm using this function from this accepted answer, which gets all values
for a custom field key (cross-post).
How could it be modified to allow an array of post statuses?
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;
}
eg something like this (doesn't work)
function get_meta_values( $key = '', $type = 'post', $status = array(
'publish', 'draft' ) ) {
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;
}

No comments:

Post a Comment