On a particular archive page I've created, I'm listing a number of posts by post name and they're listed in alphabetical order. Is there away to have them separated even further?

As in all posts starting with "A" are grouped and have a heading of "A", then all posts starting with "B" are grouped and have a heading of "B", etc.

  • FWIW I'm using a custom child theme on the Genesis framework and a custom post type and taxonomies.
    – NW Tech
    Dec 18, 2012 at 1:01
  • Could you post the code of what you've got currently? Dec 18, 2012 at 1:18
  • Here's all i have at the moment: pastebin.com/67SJ2Uvr
    – NW Tech
    Dec 18, 2012 at 3:30
  • You can customize the SQL directly using wp_query filters. Dec 18, 2012 at 4:10

1 Answer 1


You can do some tricky thing with PHP. Here is the algorithm you could use.

  1. Query posts to get posts as alphabetical order.
  2. for/while loop starts.
  3. $t = Get first character of the title string.
  4. $temp = '' empty string.
  5. if $t != $temp echo $t.
  6. set $t = $temp.
  7. endfor/endwhile.

Hope it make sense. The main idea is you check the first character of the title to a temp variable. When their is a mismatch it will print the character. So you will get a character printed out when you transit to '' to A, A to B and so on.. :)

Finally got some time. Here is a very basic code example with get_posts()


    $posts = get_posts($args);

        $temp = '';
    foreach($posts as $post):
        $title = get_the_title();
        $first_letter = strtoupper(substr($title, 0, 1)); 
        if($temp != $first_letter){
            echo $first_letter;
            $temp = $first_letter;  
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>


This code show the letter if their is an change of first letter of title. Make sure you sort it alphabetically when calling get_posts(). Hope you get the idea.

  • You code works great. However, is it possible to group the posts together under their associated letter. so all posts starting with "A" would be grouped together the heading "A". As it is right now, each post has it's own heading based on the first letter of the post name.
    – NW Tech
    Mar 21, 2013 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.