Title: Alphabetical sorting inside loop
Last modified: August 20, 2016

---

# Alphabetical sorting inside loop

 *  [titan_rising](https://wordpress.org/support/users/titan_rising/)
 * (@titan_rising)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/alphabetical-sorting-inside-loop/)
 * Hi. I have a liitle trouble with code which I found over the internet. Basicly
   I wanted list my posts by first letter of title.
 *     ```
       For example:
       A:
       Adam
       Alex
       Alvin
       Arnold
       B:
       Bilbo
       Barney
       Benjamin
       C:
       Cameron
       ....
       ....
       etc.
       ```
   
 *     ```
       <?php
       $args = array (
       	'post_type' 		=> 'post',
       	'posts_per_page' 	=> -1,
       	'numberposts' 		=> -1,
       	'orderby' 			=> 'title',
       	'order' 			=> 'ASC'
       );
       $my_query = new WP_Query($args);
       	if( $my_query->have_posts() ) {
       	echo '<div"><ul>';
       	while ($my_query->have_posts()) : $my_query->the_post();
   
       $definite_article = array('The ', 'A ', 'An ');
       $pattern = '/\b(?:' . join('|', $definite_article) . ')\b/i';
       $post_title = $post->post_title;
       $post_title = preg_replace($pattern, '', $post_title);
   
       $this_char = strtoupper(substr($trailer_title,0,1));
       	if ($this_char != $last_char) {
       	$last_char = $this_char;
       	echo '<li><h2>'.$last_char.'</h2>';
       	} ?>
   
       <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo $post_title; ?></a>
   
       <?php
       	endwhile;
       	echo '</li></ul></div>';
       	}
       	wp_reset_query();
       ?>
       ```
   
 * This code works well, but I want to add few more improvements. As you may notice,
   from post title it removes definite article such as “The”, “An” and “A”, but 
   if the post have title “A Nice…”, post listing will start from letter “N”, not“
   A”. How re-sort post alphabetically inside loop and correctly assigns letters?
 * Any kind of help will suffice.
    Thx.

Viewing 1 replies (of 1 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/alphabetical-sorting-inside-loop/#post-3506454)
 * I think you can just comment out this line:
 *     ```
       $post_title = preg_replace($pattern, '', $post_title);
       ```
   
 * like this:
 *     ```
       // $post_title = preg_replace($pattern, '', $post_title);
       ```
   
 * If this is not what you want, see this pastebin article: [http://pastebin.com/X6bPHvSY](http://pastebin.com/X6bPHvSY)

Viewing 1 replies (of 1 total)

The topic ‘Alphabetical sorting inside loop’ is closed to new replies.

## Tags

 * [alphabetical](https://wordpress.org/support/topic-tag/alphabetical/)
 * [first letter](https://wordpress.org/support/topic-tag/first-letter/)
 * [inside](https://wordpress.org/support/topic-tag/inside/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [order](https://wordpress.org/support/topic-tag/order/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/alphabetical-sorting-inside-loop/#post-3506454)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
