WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently use the following code to list posts in Archive.php but I want the results to be ordered by name in ascending order, I have checked the codex but the answer isn't clear to me, how can I get this working?

<?php $post = $posts[0]; // ?>

Thanks in advance.

share|improve this question
if you are using custom query in your archive.php can you please show it? may be post the complete archive.php on pastie.org and update your answer with the link? – Hameedullah Khan Jan 23 '12 at 10:37
up vote 10 down vote accepted

The easiest way to do this is to use a hook (the pre_get_posts hook) to change the order. But you should check that the query is one for which you do want to alter the order! (is_archive() or is_post_type_archive() should be sufficient.)

For instance, put the following in your theme's functions.php...

add_action( 'pre_get_posts', 'my_change_sort_order'); 
    function my_change_sort_order($query){
         //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
           //Set the order ASC or DESC
           $query->set( 'order', 'ASC' );
           //Set the orderby
           $query->set( 'orderby', 'title' );
share|improve this answer

further to Stephen's answer, if you want to just query and order by the title, you could use this in your template file:

$args = ( array(
'order' => 'ASC',
'orderby' => 'title',
 ) );

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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