@t31os gave a very helpful response to:
getting all values for a custom field key (cross-post) by providing this function
if ( ! function_exists( 'get_meta_values' ) ) {
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;
}
}
(slightly adapted)
I need to adapt this function to retrieve not one but two columns; meta_value
and post_id
, both from the postmeta
table and store these in an array.
I’m not knowledgeable at all with mySQL
.
Any help is appreciated.