WP: Simplify a glossary php script/code

I would like to simplify my code to avoid any repetition of the same code format. I did a custom post type of Glossary from A-Z, though i did a basic custom Query Post but it end-up to messy code output.

<?php
  $args = array (
    'post_type'       => array( 'cre-library' ),
    'posts_per_page'  => -1,
    'orderby'         => 'title',
    'order'           => 'ASC',
    'meta_query'      => array( array( 'key' => 'post_category', 'value' => 'A', 'compare' => 'LIKE', 'type' => 'CHAR' ) )
  );
  $query = new WP_Query ( $args );
  if ($query->have_posts()) :
    echo '<div id="content-for-a" class="active">';
    echo '<h2>A</h2>';
    while ($query->have_posts()) : $query->the_post();
      the_title('<h3>','</h3>'); 
      the_content();
    endwhile;
    echo '</div>';
  endif;
  wp_reset_postdata();
?>

<?php
  $args = array (
    'post_type'       => array( 'cre-library' ),
    'posts_per_page'  => -1,
    'orderby'         => 'title',
    'order'           => 'ASC',
    'meta_query'      => array( array( 'key' => 'post_category', 'value' => 'B', 'compare' => 'LIKE', 'type' => 'CHAR' ) )
  );
  $query = new WP_Query ( $args );
  if ($query->have_posts()) :
    echo '<div id="content-for-b">';
    echo '<h2>B</h2>';
    while ($query->have_posts()) : $query->the_post();
      the_title('<h3>','</h3>'); 
      the_content();
    endwhile;
    echo '</div>';
  endif;
  wp_reset_postdata();
?>

<?php //...and so on ?>

This code is working already. if we can simplify this into one loop block of PHP with having it matched post_category value per entry and display it all the post at the same time.

Related posts