Beautifully formated archives by month separated by year

Adding this snippet to the functions.php of your wordpress theme will display a beautifully formated formatted archives list of months separated by year. Don't forget to place the second snippet within your wordpress theme in the location you want to display the archives list.

function wp_custom_archive($args = '') {
    global $wpdb, $wp_locale;

    $defaults = array(
        'limit' => '',
        'format' => 'html', 'before' => '',
        'after' => '', 'show_post_count' => false,
        'echo' => 1

    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );

    if ( '' != $limit ) {
        $limit = absint($limit);
        $limit = ' LIMIT '.$limit;

    // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
    $archive_date_format_over_ride = 0;

    // options for daily archive (only if you over-ride the general date format)
    $archive_day_date_format = 'Y/m/d';

    // options for weekly archive (only if you over-ride the general date format)
    $archive_week_start_date_format = 'Y/m/d';
    $archive_week_end_date_format   = 'Y/m/d';

    if ( !$archive_date_format_over_ride ) {
        $archive_day_date_format = get_option('date_format');
        $archive_week_start_date_format = get_option('date_format');
        $archive_week_end_date_format = get_option('date_format');

    $where = apply_filters('customarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
    $join = apply_filters('customarchives_join', "", $r);

    $output = '<ul>';

        $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
        $key = md5($query);
        $cache = wp_cache_get( 'wp_custom_archive' , 'general');
        if ( !isset( $cache[ $key ] ) ) {
            $arcresults = $wpdb->get_results($query);
            $cache[ $key ] = $arcresults;
            wp_cache_set( 'wp_custom_archive', $cache, 'general' );
        } else {
            $arcresults = $cache[ $key ];
        if ( $arcresults ) {
            $afterafter = $after;
            foreach ( (array) $arcresults as $arcresult ) {
                $url = get_month_link( $arcresult->year, $arcresult->month );
                /* translators: 1: month name, 2: 4-digit year */
                $text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
                $year_text = sprintf('<li>%d</li>', $arcresult->year);
                if ( $show_post_count )
                    $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
                $output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
                $output .= get_archives_link($url, $text, $format, $before, $after);

                $temp_year = $arcresult->year;

    $output .= '</ul>';

    if ( $echo )
        echo $output;
        return $output;

Comments  Leave a Reply

  1. Cristian Florescu October 9, 2018 at 8:40 pm

    $temp_year not defined 😉

  2. Awesome code! Thanks a lot man!

    1. No problem glad I could help!

  3. Awesome code! Thanks a lot man!

  4. Awesome code! Thanks a lot man!

  5. Awesome code. Exactly what I was looking for. Do you, perhaps know how to refine it to only pull in a custom post type? *waiting hopefully 🙂

    1. post_type is defined here, “WHERE post_type = ‘post'”

  6. This is a very good example. Thanks, Now, it’s possible loading months of a specify category?

  7. Jony Fernando Schulz March 27, 2013 at 12:54 pm

    Very bealtifully. Great. Thanks!

  8. Wow. That’s a great solution without a plugin. Works perfekt. Thanks for the snippet

  9. Exactly what i needed, this should be baked into wordpress core it’s so good

  10. Great solution, thanks 🙂

  11. Dear Kevin,
    Hard to understand your script. Would you like to attach it with tabel in database MySql? May be it more clearly for me because I am new in PHP. Thanks

  12. I know this is an old post. But is there any way to specify the category of the archive list? Preferably by passing the value from the wp_custom_archive tag. So that I could put several archive lists on a page. i want an archive list for category 9 and a separate archive list for category 4. So that the tag would be something like wp_custom_archive(‘9’) and wp_custom_archive(‘4’).

    Any help?

    1. hhhmmm Hi John, many archive plugins exist that might be a better solution when wanting to get this detailed with your archives.

    2. If you’re still wondering, I’ve posted modified code above (5/29/2017) that allows for filtering by category.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!