Skip to main content

How to sort posts alphabetically in WordPress

Sorting posts in WordPress is pretty easy. Though, looking on search, there’s so many different ways to do it, including the use of plugins.

I try my best to stay away from using plugins as much as possible (I don’t like plugin bloat) – so what I do is try and code as much as I can into the template I’m creating. I may want to use a certain type of sorting order for one archive of posts, but for another archive I might want a different type of sorting order.

My easiest way is to do it like this:

Go into your archive template in WordPress, usually named “archive.php”.

Descending Post order by Title

This will make your posts start from A-Z.

<?php query_posts($query_string . '&orderby=title&order=ASC');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink();?>"><?php the_title();?></a>
<?php the_excerpt();?>

<?php endwhile; endif; ?>

Ascending Post order by Title

This will make your posts start from Z-A.

<?php query_posts($query_string . '&orderby=title&order=ASC');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink();?>"><?php the_title();?></a>
<?php the_excerpt();?>

<?php endwhile; endif; ?>

Descending Post order by Date

This will make your posts start from the latest post you’ve made.

<?php query_posts($query_string . '&orderby=date&order=DESC');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink();?>"><?php the_title();?></a>
<?php the_excerpt();?>

<?php endwhile; endif; ?>

Ascending Post order by Date

This will make your posts start from the oldest post you’ve made.

<?php query_posts($query_string . '&orderby=date&order=DESC');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink();?>"><?php the_title();?></a>
<?php the_excerpt();?>

<?php endwhile; endif; ?>

Creating paging on your archive page

Use the below code to add paging to your WordPress archive.


<div id="paging">
<?php global $wp_query; $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
     'format' => '?paged=%#%',
     'current' => max( 1, get_query_var('paged') ),
     'total' => $wp_query->max_num_pages
) );
?>
</div>

One thought to “How to sort posts alphabetically in WordPress”

  1. the archive.php can only be found in wp-content\themes\twentyseventeen\ so will that work on a different theme?

Leave a Reply