Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Alphabetizing Posts

By default, WordPress organizes and displays posts in descending chronological order. However, there are different WordPress templates you can use to list posts in alphabetical order.

Category Template

For example, you may want posts to display in alphabetical order if you have a category named "Glossary", where each post is a definition of a specific term and each term is used as the post title. Edit your theme's category.php file, and make the following changes, just before your main Loop:

<?php
get_header();
?>

<div id="content">
<ul>
<?php
// we add this, to show all posts in our
// Glossary sorted alphabetically
if (is_category('Glossary')) 
{
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
$glossaryposts = get_posts( $args ); 
}
foreach( $glossaryposts as $post ) :	setup_postdata($post); 
 ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

Note: 'posts_per_page' => -1 indicates that all posts in this category will be shown at once. You can change this number to any positive integer to limit the number of posts displayed in this array.

Specific Category Template

If you want your "Glossary" category to have a different style from the rest of your site, you could create a custom category template file. First, find the category ID of your "Glossary" category, listed in the left-most column of your Manage > Categories administration panel. For this example, we'll assume that the "Glossary" category has a category ID of 13.

Copy your theme's index.php or category.php file (or create a brand new file, if necessary) named category-13.php and insert your code:

<?php
get_header();
?>

<div id="content">
<ul>
<?php
// we add this, to show all posts in our 
// Glossary sorted alphabetically
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
$glossaryposts = get_posts( $args ); 
// here comes The Loop!
foreach( $glossaryposts as $post ) :	setup_postdata($post);  ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

Using a separate category template file for your "Glossary" category means that you do not clutter up your main category.php file with a bunch of conditional template tags.

Main Page

Maybe you want to list all your posts alphabetically by title on the main page. This means you will want to edit the main query of WordPress using pre_get_posts and is_main_query. In your functions.php file, or in a separate plugin, insert:

function foo_modify_query_order( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'foo_modify_query_order' );

This will edit the main query before it is executed, and ensure that anywhere the main query is called (in index.php for instance), posts are sorted alphabetically by title.

References