Group & Sort Taxonomy terms by letter – Is there a better way?

Here is the way I did it,

add_action('the_content', 'lm_alpha_order');

function lm_alpha_order($content){


if(is_page('alphabetique')){

    $args = array(
        'taxonomy' => 'media_tag',
        'hide_empty' => true,
        'orderby' => 'name',
        'order'   => 'ASC'
    );

    $terms = get_terms('media_tag', $args);

    $azRange = range('A', 'Z');

    echo '<div class="half left" style="float:left;padding-left: 2em;">';

    foreach ($azRange as $letter)
    {
        $letter_outputs="";
        $the_letter =false;
        //print("$letter\n");
        $count = 0;
        $ul="<ul class="alpha">";
        foreach ( $terms as $term ) {
            if ( preg_match("/^$letter/i", $term->name) ) {
                $the_letter = true;

                $args_post = array(
                    'posts_per_page'   => -1,
                    'tax_query' => array(
                        'relation'=> 'AND',
                        array(
                            'taxonomy' => 'media_tag',
                            'field'    => 'term_id',
                            'terms'    => $term->term_id,
                        ),
                        array(
                            'taxonomy' => 'media_category',
                            'field'    => 'slug',
                            'terms'    => 'audio',

                        ),
                    ),
                    'post_type'        => 'post',
                    'post_status'      => 'publish',
                    'suppress_filters' => true
                );

                $posts_array = new WP_Query( $args_post );
                $inner_letter_outputs="";
                $letter_outputs .= $ul;
                $ul="";  // only display once
                $letter_outputs .= '<li><h3><a href="https://wordpress.stackexchange.com/questions/240975/javascript:void(0);" data-toggle="collapse" data-target="#media_'.$term->slug.'">'. $term->name . '</a></h3>
                <div id="media_'.$term->slug.'" class="collapse" style="color:#000035;">';
                    foreach($posts_array->posts as $post) {
                        if ($post->ID != null) {
                            if(has_shortcode($post->post_content, 'audio')){
                               $media_url = get_mp3_url($post->post_content);
                                foreach($media_url[0] as $k=>$url){
                                    if($k == 'mp3'){
                                        $url = str_replace('"', '',$url);
                                        $media_link = $url;
                                    }
                                }
                            }

                            $inner_letter_outputs .= '
                        <span class="media_'.$post->ID.' media-title"><a href="'.get_permalink($post->ID).'">' . $post->post_title . '</a></span><br/>
                        <span class="media-button"><a href="javascript:void(0):" class="btn-play-sound" data-item="'.$post->ID.'" data-guid="'.$media_link.'" data-title="'.$post->post_title.'">
                        <i class="fa fa-play" aria-hidden="true"></i></a>  <a href="javascript:void(0):" class="btn-play-pause" data-item="'.$post->ID.'" data-guid="'.$media_link.'"><i class="fa fa-pause" aria-hidden="true"></i></a>  
                        <a href="'.home_url().'/download.php?file=".$media_link."" target="_blank" title="'.__('Download files', 'laura-marine').'"><i class="fa fa-download" aria-hidden="true"></i></a>
                        </span>
                        <br/>';
                        }
                    }
                $letter_outputs .= $inner_letter_outputs;
                $letter_outputs .='</div></li>';

                $count += sizeof($posts_array->posts);
            }
        }
        if ( $ul == '') {
            $letter_outputs .= '</ul>';
        }
        if($the_letter == true){
            echo '<h2>'.$letter . ' <small>('.$count.')</small></h2>';
        }
        echo $letter_outputs;
    }

    echo '</div>';
    $audio_shortcode = do_shortcode('
');
    echo '<div class="half right" style="position:relative; ">
            <div class="lm-sound-player" style="">
                <div class="hidden-player" style="display:none;">
                '.$audio_shortcode.'
                </div>
                <div class="player-tweet">
                    <i class="fa fa-spinner fa-pulse fa-4x fa-fw"></i><br/>
                        <span class="sr-only sound-title">Choose sound to listen...</span>
                </div>
            </div>
          </div>';

}

return $content;
}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)