Title: pulling posts alphabetically with headers
Last modified: August 19, 2016

---

# pulling posts alphabetically with headers

 *  Resolved [nadine00](https://wordpress.org/support/users/nadine00/)
 * (@nadine00)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/)
 * Hi,
 * I was just wondering how I would go about pulling posts alphabetically but with
   headers dividing them based on the alphabet?
 * So for example:
 * >  A
   >  -all posts here that start with the letter a
   > B
   >  – all posts here that start with the letter b.
   > etc..
 * thanks!

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/#post-1288978)
 * Might look at [http://wordpress.org/extend/plugins/azindex/](http://wordpress.org/extend/plugins/azindex/)
 *  Thread Starter [nadine00](https://wordpress.org/support/users/nadine00/)
 * (@nadine00)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/#post-1289083)
 * hmm. That really isn’t what I need, I need something I can just put raw into 
   a template page. so, not an archive of post titles that you can individually 
   link too, but a page of all the actual posts.
 * Is there a way to maybe do this without a plugin? Maybe multiple calls on a page?
 * Something that would fit in a set up kind of like this:
 * ‘numberposts=999&orderby=title&order=ASC’
 * like a limiter, or range.
 * Sorry, if i’m not seeing an obvious thing here. But thanks for your suggestions!
   🙂
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/#post-1289098)
 *     ```
       <?php
       $last_char = '';
       $args=array(
         'orderby' => 'title',
         'order' => 'ASC',
         'posts_per_page'=>-1,
         'caller_get_posts'=>1
       );
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         echo 'Alphabetic index of all ' . count($my_query->posts) . ' posts';
         while ($my_query->have_posts()) : $my_query->the_post();
           $this_char = strtoupper(substr($post->post_title,0,1));
           if ($this_char != $last_char) {
             $last_char = $this_char;
             echo '<h2>'.$last_char.'</h2>';
           } ?>
           <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
         endwhile;
       } //if ($my_query)
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *  Thread Starter [nadine00](https://wordpress.org/support/users/nadine00/)
 * (@nadine00)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/#post-1289115)
 * Wow thank you Michael. If I wanted to do this with posts from just one category,
   rather than all of them, would it be something like this:
 *     ```
       $args=array(
         'orderby' => 'title',
         'order' => 'ASC',
         'posts_per_page'=>-1,
         'caller_get_posts'=>1
         'in_category'=>3
       );
       ```
   
 * or like this:
 *     ```
       $last_char = '';
       $cat = 3;
       $args=array(
         'orderby' => 'title',
         'order' => 'ASC',
         'posts_per_page'=>-1,
         'caller_get_posts'=>1
         'in_category'=>$cat
       );
       ```
   
 * or maybe its something totally different.
 * Again, thanks for the query from before. Really appreciate it.
 * cheers
 * Nadine.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/#post-1289120)
 *     ```
       $last_char = '';
       $cat = 3;
       $args=array(
         'cat' => $cat,
         'orderby' => 'title',
         'order' => 'ASC',
         'posts_per_page'=>-1,
         'caller_get_posts'=>1
       );
       ```
   
 * or for multiple categories
 *     ```
       $last_char = '';
       $args=array(
         'category__in' => array(3,7,45,2),
         'orderby' => 'title',
         'order' => 'ASC',
         'posts_per_page'=>-1,
         'caller_get_posts'=>1
       );
       ```
   
 *  Thread Starter [nadine00](https://wordpress.org/support/users/nadine00/)
 * (@nadine00)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/#post-1289187)
 * bump.
 * Sorry to have to bother the forum again, but I find myself in a scenario where
   I need to sort this, and make the headers, by a custom field rather by post_title(
   client request).
 * This seems to work fine for sorting:
 *     ```
       $last_char = '';
       $args=array(
         'category__in' => array(3,7,45,2),
         'orderby' => 'guest_sort',
         'order' => 'ASC',
         'posts_per_page'=>-1,
         'caller_get_posts'=>1
       );
       ```
   
 * But I’m not sure how to figure out getting the headers to use the custom field
   as well.
    I tried this: `$post->guest_sort`
 * but that was wrong.
 * Then I tried pulling the custom filed value into a variable with get_post_meta
   but I’m probably doing it wrong:
 *     ```
       $guestSort = get_post_custom_values('guest_sort');
   
       $this_char = strtoupper(substr($guestSort,0,1));
       ```
   
 * but that didn’t work. I also tried stringing together a meta_value&meta_key=guest_sort
   string, but that didn’t work.
 *  $this_char = strtoupper(substr($post->meta_value&meta_key=guest_sort,0,1));`
 * Suggestions?
 * Thank you.
 * Nadine.
 *  Thread Starter [nadine00](https://wordpress.org/support/users/nadine00/)
 * (@nadine00)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/#post-1289188)
 * found it!
    over here: [http://wordpress.org/support/topic/248010?replies=22](http://wordpress.org/support/topic/248010?replies=22)
 * Just in case anyone comes back to this thread.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘pulling posts alphabetically with headers’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [meh_code](https://wordpress.org/support/topic-tag/meh_code/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)

 * 7 replies
 * 2 participants
 * Last reply from: [nadine00](https://wordpress.org/support/users/nadine00/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/pulling-posts-alphabetically-with-headers/#post-1289188)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
