Support » Themes and Templates » sort chronologically and then display alphabetically

  • I have a category called Writing which contains longish articles. On the front page I want to display the titles of the three most recent articles.

    However it makes no sense to display them chronologically (since the date doesn’t matter – only the fact that you might not have seen them yet).

    So I want to use the loop to retrieve the three most recent posts, and THEN have these three posts displayed alphabetically according to the post title.

    Any suggestions? Please!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Not my code because I’m not clever enough to figure it out (yet) but I have in my code snippets the following:

    In the event you want to show ten posts sorted alphabetically in ascending order on your web page, you could do the following to grab the posted date, title and excerpt:
    $postslist = get_posts('numberposts=10&order=ASC&orderby=post_title');
    foreach ($postslist as $post) : start_wp(); ?>
    <?php the_date(); echo "<br />"; ?>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

    Unless I am very much mistaken that snippet both sorts and displays alphabetically. In other words it will always find the first 10 articles beginning from “Aardvarks are ace”, even if the ten most RECENT articles all have titles starting with “S” or “T”.

    I need to sort chronologically (in this example, sort the 10 most recent, all of which begin with “S” or “T”), and THEN display them alphabetically like this:

    Samba for beginners¨
    Sentences in French
    Simple Maths
    Tadpoles are fun
    … and so on.

    If there is a one-line solution for this I have failed to find it yet. The more complicated approach would be to retrieve them into an array, then sort the array, and then display them.

    Is there an easier way, using built-in functions?

    I got that code from here
    Is it possible to do this with the parameters on that page?


    Given richarduk’s start, you are creating an array called $postslist. Instead of packing the array the way he does, pack it with the # of recent that you want. Then sort the postlist array itself using the array.sort() method. So in short, let the database do the date sorting and then let PHP do the alphabetizing.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘sort chronologically and then display alphabetically’ is closed to new replies.