WordPress challenge of the day: sorting by meta value, including posts WITHOUT that meta value set

I dug around quite a bit for a solution to this today, and eventually I found one, even though it’s a bit ugly.

The problem here is in setting up a WordPress query that sorts posts based on a meta value. I wanted to sort a list of pages by template, but I wanted to include all of the pages, even ones that don’t explicitly have a template set. But the default query was only showing the ones that did have the template value.

Several dead ends almost led me to give up, until I realized it was a JOIN issue. Specifically, the need to change an INNER JOIN to a LEFT JOIN. I just needed to figure out how to do that in the context of WP_Query.

Cut to the chase, here’s what I ended up with.

add_action('pre_get_posts', function($query) {
  if (!is_admin()) { return; }
  $orderby = $query->get('orderby');
  if ('_wp_page_template' == $orderby) {
    $query->set('meta_key','_wp_page_template');
    $query->set('orderby','meta_value');
    // Workaround to include items without this meta key
    // Based on: https://core.trac.wordpress.org/ticket/19653#comment:11
    add_filter('get_meta_sql', function($clauses) {
      $clauses['join'] = str_replace('INNER JOIN','LEFT JOIN',$clauses['join']) . $clauses['where'];
      $clauses['where'] = '';
      return $clauses;
    });
  }
});

I don’t like doing a str_replace() on part of the pre-built query, but sometimes you gotta do what you gotta do. Also note that this is part of a larger function I am writing that is only for use in the admin side; you could remove that is_admin() check if you want this to work everywhere.

I haven’t had a chance to dig into the details of the query to figure out why the original source post included moving $clauses['where'] into $clauses['join'], but it’s essential. I tried skipping it, and it didn’t work.

Sorry I can’t provide any more context here… but I hope it’s helpful to anyone who finds themselves in a similar situation!

Great Dieter Rams interview

Dieter Rams, legendary industrial designer from Braun in the 1950s and ’60s, pretty much invented my design aesthetic. I can’t imagine what the world of technology would look like without his pioneering work. Brilliant.

I especially like his design principle #10: “Good design is as little design as possible.”

I probably didn’t hear of Dieter Rams until about ten years ago, but I’ve seen the world through his eyes since I was a kid. Most of these Braun products were not readily available in the U.S. in those days (the late ’70s and early ’80s), but their designs were so influential that just about everything you could get here still looked like them, or pale imitations thereof.

Jason Kottke also tidily sums up Rams’ influence on modern industrial design:

And hey, I didn’t know that a book had been published on Rams’ work. I bet Jony Ive has at least three copies.

I’ll take one of everything, please.

Source: Monoscope via kottke.org

Yes, it has been colder in Minneapolis this summer… except when it wasn’t

There’s a bit of a brouhaha afoot with regard to our weather in Minnesota this summer, and whether it proves or disproves climate change.

A good summary of the “debate” appeared yesterday on Alas!

It started with a Minneapolis-based wingnut blogger relying on anecdotal evidence to prove… something.

Statistics guru Nate Silver responded with a bunch of boring old facts that dispel the argument of a colder-than-normal summer.

I just have a few comments to add to the fray:

1. If climate change is real (and it’s pretty much impossible for an honest, rational person to deny at this point), anecdotal evidence of a chilly month of July in one city doesn’t do anything to disprove it. And if you’re not looking at hard numbers, it’s easy to endure this cold July and forget just how hot it really was at the end of June.

2. Rising global temperatures associated with climate change emphatically do not mean that the resulting weather change in any particular location will manifest as a simple 2-3 degree temperature increase, and identical weather as before. In fact what it means is that global weather patterns will change significantly, and unpredictably, with some parts of the globe experiencing significantly hotter temperatures, some cooler, and more severe weather events occurring in more places than before.

OK, I’m a geek, but this was totally cool! (In a geeky way…)

Ever since I read Word Freak, an exposé on the world of competitive Scrabble by Wall Street Journal sports reporter Stefan Fatsis [wow, such a convoluted sentence, simply to avoid having to write “Fatsis’s”], I’ve been obsessed with improving my Scrabble game. (Excuse me, my SCRABBLE® Brand Crossword Game… er… game.)

For a couple of nights, SLP was into it (why does that sound dirty?), but she just couldn’t match my endurance (again… why?). So I had to resort to playing against the computer. I’ve been playing tournament style to boost my (unofficial) rating, playing mostly against the 1220-rated “Veteran” (whom I beat about 2/3 of the time) and the 1400-rated “Smart” (to whom I lose about 2/3 of the time).

Tonight my rating finally topped the 1300 mark (against Smart, no less), and I celebrated by playing one more game against Veteran. And therein came my greatest moment.

Veteran had set up the triple word column on the right edge with LAVE, and my draw that turn included the Q, the Z, and both blanks! I stared at the board for a moment before realizing I had a most unusual play (if it was actually a word). And so it was that I laid down QUIZZER through the E in LAVE, with the Q on the triple word and the (real) Z on the double letter, using the blanks for the U and the other Z, giving me 99 points. If only I’d had an S on my rack, I could have hit the other triple word as well, for a triple-triple-bingo, worth 356 points! (That’s among the highest possible scores for a single word, even though, as a fairly pedestrian word, it doesn’t carry as much cachet among Scrabblers as words like QUIXOTIC or MEZQUITE.)

I still ended up winning the game with several other high-scoring moves, including a ballsy (if anything Scrabble-related is ever even remotely “ballsy”) multi-turn set-up that allowed me to play EXITS on a triple word for 49 points, after having already milked the X for all it was worth. I almost screwed it up though. I had played LURID early on, to which I later added the double XI. Already I was planning EXITS, but I was missing the T. So to try to build it up even more, I played RIDE while waiting for the T, but… d’oh! I really should have just played RID, because I needed that E! Naturally the T landed on my rack in the next turn, but we were getting close to the end of the game, and I didn’t have an E! Without counting, I assumed they were all on the board already, but I got one in my second-to-last draw, and EXITS appeared! On the final turn, I was left with AINRT on my rack, which fit nicely in the same area to turn ER into TRAINER for the victory! 447 may be my highest single game score ever.

Yeah, I’m a geek. But I represent! (Saying that makes me even more of a geek, doesn’t it?)

Wow, looking back at that screenshot, I’m even more impressed with myself (if that’s possible). Tournament play uses a clock, just like chess, with each player limited to 25 minutes total (going over the time limit carries a steep penalty at the end of the game). When I first started playing computer Scrabble a couple months back, I’d usually use up almost all of my 25 minutes, but in this game my clock read 17:34 at the end, meaning I had only used 7 minutes and 26 seconds for the entire game! Of course, as usual, Veteran only used 15 seconds. One time I think the computer only used four seconds for the whole game. I think the computer needs a handicap on the tournament clock: the player gets 25 minutes and the computer gets 25 seconds. Yeah, that sounds fair.

I’ll stop now. If I go on, I may just have to beat myself up.