WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sort posts by year and alphabetically (7 posts)

  1. rodrego
    Member
    Posted 4 years ago #

    I need to sort posts in a very particular order:

    Yearly (and not by date), and inside the same year, alphabetically. Is this possible? How can I do it?

    Thanks in advance!

  2. MichaelH
    Member
    Posted 4 years ago #

    1. Use get_posts to get latest post date and year of that post
    2. Use get_posts to get oldest post date and year of that post
    3. Iterate through those dates latest to oldest (or oldest to latest)
    3a. Use new WP_query loop to get posts for that year, sorted by title

  3. rodrego
    Member
    Posted 4 years ago #

    Hi Michael, thanks for the answer! Do you mind to show me an example of the code? I'm not really a programmer, what do you mean by "iterate"? Thanks again

  4. MichaelH
    Member
    Posted 4 years ago #

    <?php
    $args=array(
        'orderby' => 'date',
        'order' => 'ASC',
        'posts_per_page' => 1,
        'caller_get_posts'=>1
    );
    $oldestpost =  get_posts($args);
    
    $args=array(
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => 1,
        'caller_get_posts'=>1
    );
    $newestpost =  get_posts($args);
    
    if ( !empty($oldestpost) && !empty($newestpost) ) {
      $oldest = mysql2date("Y", $oldestpost[0]->post_date);
      $newest = mysql2date("Y", $newestpost[0]->post_date);
    
      for ( $counter = intval($oldest); $counter <= intval($newest); $counter += 1) {
    
        $args=array(
          'year'     => $counter,
          'posts_per_page' => -1,
          'orderby' => 'title',
          'order' => 'ASC',
          'caller_get_posts'=>1
        );
    
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '<h2>Posts for ' . $counter . '</h2>';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><small><?php the_time('F jS, Y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
            //the_content('Read the rest of this entry &raquo;');
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
      }
    }
    ?>

    And please, no question on pagination ;)

  5. rodrego
    Member
    Posted 4 years ago #

    Hi Michael. The code worked perfectly. Thank you again!

  6. Organix84
    Member
    Posted 4 years ago #

    Thank you for the code, it is very helpful! Just one question, when the "POSTS" post to the page, 2008 appears before 2010 going down the page. How do I flip these two, ive been messing with you code and cant seem to come up with the proper solution. Thanks!

  7. Organix84
    Member
    Posted 4 years ago #

    Never mind I created a new solution that will start with the newest year.
    Replace:

    for ( $counter = intval($oldest); $counter <= intval($newest); $counter += 1)

    With:

    for ( $counter = intval($newest); $counter >= intval($oldest); $counter = $counter - 1)

    Thank you for your work, it is greatly appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic