I'm attempting to create a custom archive page ordered in breakable columns (depending on the resolution). Ideally something that is as simple as possible under the hood.

So: What would be the best way to recreate something like this using a Wordpress archive query?

enter image description here

Thank you!


The best way is to make a SQL query to the database and get the results like the wp_get_archives() does but now you can manipulate the results as you wish.

In your case I would do something like this I added some comments to help you understand the process.

function get_archives_lists() {
    global $wpdb, $wp_locale;

    // get year month array
    $monthly = $wpdb->get_results("
        SELECT YEAR(post_date) AS year, MONTH(post_date) AS month
        FROM $wpdb->posts
        WHERE post_type='post' AND post_status='publish'
        GROUP BY YEAR(post_date), MONTH(post_date)
        ORDER BY post_date DESC
    ", ARRAY_A);

    // Rearrange the results to be [year][] = month so it will be easy to render
    $arr = [];
    array_map(function($item) use (&$arr) {
        $arr[$item['year']][] = $item['month'];
    }, $monthly);

    // loop years
    foreach($arr as $yearKey => $year) {
        <div class="col-sm-2">
            <h2><a href="<?php echo get_year_link($yearKey); ?>"><?php echo $yearKey; ?></a></h2>
            // loop 12 for monthes
            for($m = 1; $m <= 12; $m++) {
                // get month name
                $month = sprintf( __( '%1$s' ), $wp_locale->get_month( $m ) );
                    // check if the month is in our results so we add a link to it.
                    if(in_array($m, $year)) {
                        <a href="<?php echo get_month_link($yearKey, $m); ?>"><?php echo $month; ?></a>
                    } else {
                        echo $month;


You can see I used the bootstrap class col-sm-2 just for example you can use your own class and css.

  • Once again, thank you very much for this. It looks perfect. Quick question tough: Is there any way to omit months where there are no posts? As it stands, the month names with no posts appear but are of course just text and not links. I'd like to exclude them altogether. Or would that interfere with the uniform length of the columns and end up looking weird for years where there are only 3-4 months of posts? Thanks again. – eightnaut Aug 30 '18 at 9:24
  • @eightnaut Yes you can move the condition before the <li> tag open and close and put the <li> inside the condition and remove the else. You can try and see if you like it and its not look weird.. – Shibi Aug 30 '18 at 11:07
  • Cool! That works. Also, finally, what would be the easiest way to use smaller col sizes for mobile with the code you've already written? On my desktop, it fits four columns which looks nice. Mobile it's just one. It'd be nice to at least have two columns side-by-side. Thank you for all your help. – eightnaut Sep 2 '18 at 2:27
  • You can add the class col-xs-6 in bootstrap to the div – Shibi Sep 2 '18 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.