WordPress.org

Ready to get started?Download WordPress

Forums

sort chronologically and then display alphabetically (5 posts)

  1. owenkelly
    Member
    Posted 7 years ago #

    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!

  2. richarduk
    Member
    Posted 7 years ago #

    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:
    
    <?php
    $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; ?>
  3. owenkelly
    Member
    Posted 7 years ago #

    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?

  4. richarduk
    Member
    Posted 7 years ago #

    I got that code from here

    http://codex.wordpress.org/Template_Tags/get_posts
    Is it possible to do this with the parameters on that page?

    get_posts('numberposts=10&orderby=post_date&orderby=post_title');

  5. ruzel
    Member
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags