List categories year by year in wordpress mu

We have installed a multi-WP for a school. They have a need to display the list ordering, year by year, the categories used that year. For example:

They could tell me the code to implement this in a template or plugin?

Related posts

Leave a Reply

1 comment

  1. Although this will output categories (each post will have its category with it) a bit different from the format that you have mentioned above, but this will get you started:

    <?php
    // get years that have posts
    $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type =   'post' AND post_status = 'publish' GROUP BY year DESC" ); //assuming that you have default wordpress table prefix
    
    foreach ( $years as $year ) {
    // get posts for each year
    $posts_this_year = $wpdb->get_results( "SELECT post_title FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" ); //assuming that you have default wordpress table prefix
    
    echo '<p>Issues of ' . $year->year . '</p><ul>';
    foreach ( $posts_this_year as $post ) {
        $category = get_the_category( $post );
        $post_url = get_permalink( $post->ID);
        echo '<li>' $category[0]->cat_name.' | '. '<a href="'.$post_url.'">'.$post->post_title . '</a></li>';
    }
    echo '</ul>';
    }
    ?>