How to Display Posts by Alphabetical for Each Letter – WordPress

Alphabetical order posts wordpress

To display the posts by alphabetical for each letter it’s very easy to do this. We can do this also through custom post type as well. In this tutorial we can learn how to display the posts by Alphabetical for Each Letter. Put the below codes in the file where you want to display the posts by Alphabetical for Each Letter on wordpress.

* Here we used it through custom post type.  The custom type is: “transaction“. Check the below code. You can edit  the custom post type as per the requirement.

<?php $alphs=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
foreach($alphs as $alph) {
echo '<div class="group-sec">';
//echo $alph;
?>
<ul class="posts">
<?php
global $wpdb;
$request = $alph; // could be any letter you want
$results = $wpdb->get_results("
SELECT * FROM $wpdb->posts
WHERE post_title LIKE '$request%'
AND post_type = 'transaction'
AND post_status = 'publish';
");
//echo count($results);
//print_r($results);
if ( $results ) {
foreach ( $results as $key => $result ) {
?>
<li>
<h3>
<?php if($key==0){
echo $alph;
} ?></h3>
<section class="v-center">
<div>
<a href="<?php the_permalink(); ?>" id="<?php echo $result->ID; ?>"><?php echo $result->post_title; ?></a>
</div>
</section>
</li>
<?php }
} ?>
</ul>
<?php
echo '</div>';
}
?>

Display Posts by Alphabetical for Each Letter