in code

I spent today writing a two thousand word article on regular expressions. Symbols dance around when I close my eyes. Kill me now.

x to y of z search results II

in code

The code snippet I posted last August hasn’t worked too well:

  • It only works for the index.php loop of all posts.
  • All other loops (search, taxonomy, archive, etc.) have weird counts.

I changed it to use the global wp_query variable to determine post count, with an addendum for private posts if I am logged in. get_loop_type is a shorthand function I wrote a while ago.

function archive_page_count($echo = false, $page_num = null, $total_results = null) {
    global $wp_query;

    if (is_null($total_results)) {
        $total_results = $wp_query->found_posts;

    if (is_null($page_num)) {
        $page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $posts_per_page = get_option('posts_per_page');
    $total_pages = ceil($total_results / $posts_per_page);
    $page_count = sprintf(__('Page %s of %s', TTD), $page_num, $total_pages);

    if (!$echo) {
        return $page_count;


All hail the glorious vim master race

in code

OR: “Moving from Sublime Text 2 to vim.”

Click here to revel in my glorious vim master race desktop.

Dual head vim setup

I have used Sublime Text since 2011. I don’t know when I first used it, beyond clear memories of it open in class during my first year at I.T. Sligo. I asked “suggest a prettier editor than Notepad++” on Facebook, and received a link to Sublime. The rest, as the cliché goes, is history.

I have used vim alongside other GUI editors since ~2001. I have used:

  1. gvim.
  2. Kate in vi mode.
  3. Sublime Text in vintage mode.
  4. Vimium in Google Chrome.

And so on, ad infinitum; vim-style modes and bindings are both popular and easy to shoehorn into any software at all with an API.

Sublime Text is awesome; Sublime is flexible, extensible and packed with all sorts of great features. I have relied on Sublime and the Sublime SFTP plugin since I began work on with Tuairisc in August 2014. Sublime SFTP is, like Sublime, awesome: not only has it support for FTP, but also SFTP, FTPS and straight SSH connections. It can sync files and folders in both directions. It’s easy. The problem I have is that, in the end, my dependence on vintage mode and Sublime FTP has led to bad habits:

  1. Why version control and sandbox the test code when I can just upload shit and see what runs?
  2. Why bother to learn vim in depth when Sublime Does a Good Enough Job?

Both these and other things have led me to reassess Sublime over the past few weeks:

  • Many of Sublime’s keybinds aren’t friendly on my wrist in OS X. I fucked my right arm with RSI when I was bedridden during my 2012 illness. I have been careful about mouse use since then, to the point that I keybound everything in World of Warcraft. While I know I’m free to change every keybind in Sublime Text, that is a time-intensive exercise I don’t relish.
  • Sublime Text and Sublime SFTP are nagware and I’m cheap.
  • Sublime has become an intermediary between my true workspace and I. When I work, I feel like I am building a ship in a bottle while I wear oven mitts. Chris Hadfield’s tales of working in thick spacesuit gloves is a good allegory.

So as of Monday I went all-vim, all the time. I now work on my server through SSH, screen and tmux. I dove in headfirst as I yodeled “YOLO”. These tools allow me to now work on my code with vim, manage commits with tig and save a large amount of time. Neither do I have to fuck around with GUI applications anymore.

Here is what I have learned:

  • Having a keyboard and mouse with which to copy and paste makes one lazy, but it is also slow. You can see what you need to copy and where you need to paste it, but you don’t look at their positions. You click first on one and then the other. vim-for lack of a better description-forces you to be aware of the spatial position of characters in the file. What do I need to copy? What are its line and column numbers? Where is the target area in relation to this? How many lines and columns away is the destination?
  • vim isn’t as scary as I feared. I mean, fuck, whenever I think of vim, I conjure up a hoary veteran of the of the kernel mailing lists. But additions to the .vimrc (here’s mine) have proved straightforward. Adding new plugins and colour schemes (oxeded) has proved to be as simple as untarring them into the respective folder.
  • Macros are a lifesaver. To give an example, I use the phpdoc style of code comments. I am able to save and then paste the same boilerplate over and over without having to type it out for the nth time.

vim has been good to me and this move has proven good for me.


Get an array of per-year and per-month counts of your WordPress posts

in code

PHP archive function

I had to research this for a client. Their project requires a list of posts per month and per year over the duration of the blog but, like mine, they have a lot of posts spread over an extended period of time. It would benefit me to have the same count, so I approached a solution for both sites. As of this article, I have published just shy of 1800 posts over an inclusive 11 years (2005-2015). There are a few methods to grab a post count for the current month, or for a given month using WP_Query or get_posts. These methods take up to a second to iterate for a blog of my age and and size, which is just too slow. While I could save the results using update_option, that bring in date checking, incremental generation, variable validation and a host of overhead that wouldn’t work as well as an efficient method.

This Stackoverflow thread gave me the seeds of a solution, which I expanded into a function that:

  1. Determines the year of the first post.
  2. Determines the current year.
  3. Inclusively iterate through each year using the ‘$wpdb‘ global variable to find the number of posts for each month of that year. A month isn’t added if there were no posts during it.

I’m going to assume that most calendar archive widgets use something like this. Thing is, Google didn’t turn up anyone who talked about an easy and copy-pastable solution, so here you go:

GitHub gist!

The function returns a multidimensional array ordered by year in descending order, with each month added by name followed by a count.

 * Convert Number to Month
 * -----------------------------------------------------------------------------
 * See: https://stackoverflow.com/questions/18467669/convert-number-to-month-name-in-php
 * @param  int          $number             The month of the year as a number.
 * @param  string                           The month as a word.

function get_month_from_number($number) {
    return date_create_from_format('!m', $number % 12)->format('F');

 * Generate Dated Archive Post Count
 * -----------------------------------------------------------------------------
 * Generate the initial count of posts by year and month, and save it under the
 * given options key. Generating this can be resource intensive, so it makes 
 * sense to store this as a variable.
 * See: https://wordpress.stackexchange.com/questions/60859/post-count-per-day-month-year-since-blog-began
 * @param   string      $option_name        Options key for the post count.
 * @return  array       $counts             Returned counts for the 

function timed_archives_count() {
    global $wpdb;

    /* Get the year of the first post: 
     * -------------------------------------------------------------------------
     * 1. Get 1 post in ascending order. This is the first post on the blog.
     * 2. Extract the date of the post.
     * 3. Parse that down to the year alone. */
    $from_date = preg_replace('/-.*/', '', get_posts(array(
        'posts_per_page' => 1,
        'order' => 'ASC'

    for ($i = date('Y'); $i >= $from_date; $i--) {
        $counts[$i] = array();

        $month = $wpdb->get_results($wpdb->prepare(
            "SELECT MONTH(post_date) AS post_month, count(ID) AS post_count from " .
            "{$wpdb->posts} WHERE post_status = 'publish' AND YEAR(post_date) = %d " .
            "GROUP BY post_month;", $i
        ), OBJECT_K);

        foreach ($month as $m) {
            $counts[$i][get_month_from_number($m->post_month)] = $m->post_count;

    return $counts;

Here is a snippet of how the returned array appears:

array(5) {
    string(1) "8"
    string(2) "14"
    string(2) "12"
    string(2) "17"
    string(1) "6"

There’s lots more I can do from this nub of a function. Quick example that spits out an ordered list:

$archive_counts = timed_archives_count();

foreach ($archive_counts as $year => $months) {
    printf('<br />%s<br />', $year);

    foreach ($months as $month => $count) {
        printf('%s: %s<br />', $month, $count);