How to Group Articles or Posts Alphabetically

To help your site visitors quickly find one or two posts, you can create a list post ordered by date, category, or alphabet. There are tons of tutorials you can find to create this list. But this tutorial is an advance version to list post ordered by alphabet ;)

How to Group Articles or Posts Alphabetically

The post in this list will be grouped by it’s alphabet, so the list will be like this:

A.
Afirst post
Alorem Ipsum

B.
Bolot sit amet
Bconsectetur adipisicing

etc..etc

How to

Ok, here’s the code :


<?php
$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;
} 
wp_reset_query();
?>

You can put this code in a page template to create a site map.

Hope this code helps.

Credit:
Thanks to khamtv and MichaelH from WordPress.org

Save your time and get on time success in 6401-1 exams by using our latest 642-902 and other superb exam pass resources of 70-642.

13 Comments to “How to Group Articles or Posts Alphabetically”

Add Comments (+)

  1. Fredrik says:

    That was a fast and nice tip! thx for that one… keep em coming!

  2. Pablo says:

    Thanks for your help!
    PS. How can I get the first letter was UTF-8, not a strange sign?

  3. Pablo says:

    :)

    
    <?php
    $args=array(
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1,
      'post_type' => 'autorzy'
    );
    $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(mb_substr($post->post_title,0,1,'UTF-8'));
        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;
    } 
    wp_reset_query();
    ?>
    
    
  4. Pablo says:

    Show me this list:
    A
    D
    K
    P
    W
    K
    P
    Ś

    What is wrong?

  5. Donald says:

    How to show only posts that starts with, for example, the letter C ?

    • reza says:

      try this

      
      <?php
      
      //get all post IDs for posts beginning with cap C, in title order,
      //display posts
      $first_char = 'C';
      
      $postids=$wpdb->get_col($wpdb->prepare("
      SELECT      ID
      FROM        $wpdb->posts
      WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
      ORDER BY    $wpdb->posts.post_title",$first_char)); 
      
      if ($postids) {
      $args=array(
        'post__in' => $postids,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
       echo 'List of Posts Titles beginning with the letter '. $first_char;
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
      wp_reset_query();  // Restore global post data stomped by the_post().
      }
      ?>
      
      
  6. Jon says:

    Hello,

    Thanks for the helpful code. I was wondering if it would be possible to sort and group by a custom field value instead of by post title?

    Is it possible to modify this line:

    $this_char = strtoupper(mb_substr($post->post_title,0,1,’UTF-8′));

    With something instead about a custom field value? I tried modifying the query args to get meta values and orderby meta value, but it messed up the order below.

    Thanks!

  7. Hi,
    I wondered if i could add an rule :
    when the article starts with The, then use the first letter of the second word.
    Example:
    if the title of the article is “The Article”
    instead of appearing at letter T, appear at letter A

    Any help?

    Thx

    Daniel

Trackbacks/Pingbacks

  1. How to Group Articles or Post by Author
  2. 70 Excellent WordPress Tips And Tutorials From First Three Months Of 2011

Leave a Reply

 

Amazingly Beautiful WordPress Themes