robinadr

Although version 2.6 was just released less than a month ago, I’m proud to announce the release of Smarter Archives 3.0. As the version number would suggest, this is a major update.

I’ve completely rewritten the code while maintaining backwards compatibility, resulting in more streamlined code, and also reducing the total number of database queries used to 1. As in a single query. More on the code differences can be found later in this post.

Go to the plugin page to download it now, or upgrade from within your WordPress dashboard.

“More than one way to skin a pig”

The last version introduced the smarter-archives shortcode as a quick and easy way to insert the archive listing anywhere. The built-in function has various options for customizing the output, but I get that some people might want to customize it even further.

This is where get_smarter_archives() comes in. By separating the database and presentation code, you can skip the presentation part and just process the archive array on your own. It doesn’t get more customizable than that.

Just one query

Here’s how the plugin interacted with the database before:

  • 1 query to get every year that had posts
  • For every month in each years, query to see if that month had posts

The end result was 1 + (12x) queries, where x is the number of years the blog posts span. In my case, as I’ve been blogging since 2007 on this site, I ended up with 85 queries.

Now it’s all down to one:

~~~ {.prettyprint .lang-sql} SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = ‘post’ AND post_status = ‘publish’ GROUP BY MONTH(post_date), YEAR(post_date) ORDER BY year DESC, month ASC



For me, that's **85 times fewer queries**. I haven't run benchmarks or anything, but I don't think I really need to...

The translation files have also been updated, including the Spanish translation strings.