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.

  • 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? Commented Jan 23, 2012 at 10:37

3 Answers 3


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' );
  • Hi, will you be able to show how does the default ordering works? Some links if possible .thanks
    – user145078
    Commented Sep 7, 2019 at 21:34
  • @LatheeshVMVilla WP is developed as a blog, so the sensible/default ordering is by post_date DESC (=descending), so that's newest-post-first. If you're using WP for something where time is less relevant (most types of lists, like your records collection, recipes, a glossary, ...) you'll want to order often post_title ASC (=Ascending, so alphabetic by title with numbers preceding letters). Commented Dec 3, 2019 at 13:25
  • Thanks. Works fine for my taxonomy archive page. Commented Feb 5, 2020 at 16:12
  • Worked perfectly. Thank you!
    – Mark P
    Commented Jun 17, 2020 at 18:59
  • Worked but destroyed my menu. There are more queries on the archive page, to change just the one needed, I succeeded with if(is_post_type_archive($post_type) && $query->query["post_type"]==$post_type)
    – Fanky
    Commented Aug 26, 2021 at 18:10
// 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; ?>
  • 1
    Can you please explain why this helps the OP? Please, always add an explanation on top of a piece of code. Thanks.
    – kaiser
    Commented Jan 20, 2017 at 23:12
  • The question was Sort results by name & asc order on Archive.php. Presumably according to the answerer this code will Sort results by name & asc order on Archive.php?
    – Jon
    Commented Dec 11, 2019 at 14:37

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',
 ) );

  • 5
    Straight from the WordPress code reference - "This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. In most cases, there are better, more performant options for modifying the main query such as via the ‘pre_get_posts’ action within WP_Query." Bottom line @Stephen Harris has the correct way of accomplishing this. developer.wordpress.org/reference/functions/query_posts
    – Michael
    Commented Oct 13, 2016 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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