What I am attempting to do would be to operate a query to drag back all surnames produced utilizing a custom publish key in wordpress. The code I've is the following:

$surnames = $wpdb->get_results( "SELECT DISTINCT pm.meta_value 
                        FROM wp_postmeta pm
                        INNER JOIN wp_posts po ON po.ID = pm.post_id
                        WHERE pm.meta_key = 'bl_surname'
                        AND po.post_status = 'publish'
                        ORDER BY pm.meta_value ASC" );
$surnames = array();

foreach ($surnames as $surnames) {

    $surnames[$surnames->meta_field] = $surnames->meta_value;


Thanks, I am unsure if I am heading down the best road right now and may do so with a few suggestions about this.