How To

Reordering Facet Displays … For Death

Using FacetWP to tackle multiple deaths, sort ordering, and of course, Sara Lance.

Isn’t that a catchy title?

I’m using FacetWP to help me order and sort archives in ways that are reflective of the content. One of the things I sort are characters (yeah yeah yeah) and some of those characters are dead. It occurred to me that wouldn’t it be nifty if I could sort the characters by when they died?

There was just one problem. Actually there were two. One was Sara Lance, and she’s my personal demon. The other was my own stupidity and lack of foresight. Neither were insurmountable.

How FacetWP Changes Sort Order How FacetWP Changes Sort Order

Before I get into the weeds, let’s have a moment to talk about sort order. FacetWP has a way to filter the sort orders.

So for an example, I have post meta value for the number of characters saved as lezshows_char_count for all shows. If I wanted to sort shows by the most characters to least, I can add this in:

$options['most_queers'] = array(
	'label' => 'Number of Characters (Descending)',
	'query_args' => array(
		'orderby'  => 'meta_value_num', // sort by numerical custom field
		'meta_key' => 'lezshows_char_count', // required when sorting by custom fields
		'order'    => 'DESC', // descending order

It looks very similar to WP_Query and that’s what makes it easy. Except for my two problems…

Top ↑

Problem One: Formats Problem One: Formats

The first problem was not the Sara Lance problem. It was the ‘Mika didn’t think about things 4 years ago’ problem. I was saving the dates of death in the format of MM/DD/YYYY

If you’re an American, you’re wondering “So what?” and if you’re anyone else, you’re giving me a death glare because “08/05/2010” could be August 05 OR May 08, and damn it, I knew better. For what it’s worth, the output on the front end is always “05 August 2010” but that’s not here nor there.

You see, the issue isn’t that I was using stupid date/time formats, the issue is sorting.

In the previous example, I have an order of meta_value_num which is literally a number. What’s the one for dates? You get meta_value_date or meta_value_datetime and neither of them work with the date format I’d chosen.

So for this to work, I had to go and change everything from MM/DD/YYYY to YYYY/MM/DD – Not fun, but doable. And it led me to my Sara Lance Drama…

Top ↑

Problem Two: Arrays Problem Two: Arrays

How many times has Sara Lance died? Right now, three.

When I decided to sort by the date of death, which one did I pick? Long pause.

I decided to pick the last one. That is the most recent death. If someone’s actually still dead, the most recent death is the one that stuck. If they’re not, then death was pretty arbitrary to begin with and there ya go.

The question became how and where did I save the death? I went with a post meta of lezchars_last_death and had it auto update on post save, like this:

add_action( 'save_post_post_type_characters', 'characters_update_meta', 10, 3 );
function characters_update_meta( $post_id ) {

	// unhook this function so it doesn't loop infinitely
	remove_action( 'save_post_post_type_characters', 'characters_update_meta' );
	// get the most recent death and save it as a new meta
	$character_death = get_post_meta( $post_id, 'lezchars_death_year', true );
	$newest_death    = 0000-00-00;
	foreach ( $character_death as $death ) {
		if ( $death > $newest_death ) $newest_death = $death;

	if ( $newest_death !== 0000-00-00 ) {
		update_post_meta( $post_id, 'lezchars_last_death', $newest_death );

	// re-hook this function
	add_action( 'save_post_post_type_characters', 'characters_update_meta' );

If there is a latest death, we get to set it as the YYYY-MM-DD value and off we go. But…

Top ↑

Problem Three: Orderby Hellscape Problem Three: Orderby Hellscape

Surprise! I ran into a third problem! Remember how I was using the orderby of meta_value_num in my example? And I mentioned that I wanted to use meta_value_date or meta_value_datetime to sort by date?

Yeah no.

If I’d converted the date into unix time, sure. But I was reusing this logic in a couple places, and I didn’t want to re-save everything like that. I also use a query to grab all deaths in a year, and basically I do need to keep it with the format I have. That just messed up my sort until I found the magic of orderby' => 'meta_value',

Top ↑

End Result? End Result?

It works. It’s got yet another post meta, which I’m not super happy about, but sometimes that’s really just the simplest way to solve a problem. The data is always updating itself, and it’s relatively easy for me to tweak it. Also now I can do a lot more searches in different ways.

Since I don’t have to worry about database size at the moment, nor speed, since I’ve designed it well, this works for me.

2 replies on “Reordering Facet Displays … For Death”

Isn’t year-month-day YYMMDD or YYYYMMDD the ISO format for dates? Beyond that, I find the problem you’re trying to solve for is awesome.

Comments are closed.