One of the things I’ve been working on over on my tv project was getting better lists of characters who died. Initially all that we recorded was the year. However we started getting the idea of a timeline and a calendar and a ‘this year in queer TV’ section, and I realized I needed more precise data.

I’m going to skip the headache part here, but I changed the year dropdown to a calendar date field that stored the date as MM/DD/YYYY. While some calculations would have been easier with the time saved as a UNIX timestamp, having it be text let me search as text to collect all the death for a year. There were pros and cons to both sides of the coin of how to save the data. The other trick was I needed to save the data as an array. Just in case someone died twice.

Suffice to say, here’s your scenario. I have meta data in the post containing the date of character death, and I want to display it in a specific way, not supported out of the box by the default queries.

Display all the dead in a list ordered by date

The simple part in WordPress is to get a loop of all posts in a type that have the meta for death. Using WP_Query I grabbed the array and instead of saving all the post’s data, I extracted the data I needed:

  • character name and URL
  • date of death
  • list of shows and their respective URLs

This I turned into a very simple array that I can use in multiple situations:

$dead_chars_loop = new WP_Query( array(
	'post_type'       => 'post_type_characters',
	'posts_per_page'  => -1,
	'meta_query'      => array(
			'key'     => 'chars_death_year',
			'value'   => $thisyear,
			'compare' => 'REGEXP',
) );
$dead_chars_query = wp_list_pluck( $dead_chars_loop->posts, 'ID' );

foreach( $dead_chars_query as $dead_char ) {
	$show_info = get_post_meta( $dead_char, 'chars_show', true );
	$died = get_post_meta( $dead_char, 'chars_dead', true );

	$output_array[$post_slug] = array(
		'name'  => get_the_title( $dead_char ),
		'url'   => get_the_permalink( $dead_char ),
		'shows' => $show_info,
		'died'  => $died,

This array is made by looping through those WP_Query results and, for each character, grabbing the post meta data for shows and death. The simple array $output_array contains only the data I need for display. So no post content, no author, nada. Just the name, the URL, the shows, and the death. That said, I’m omitting my insane extra loop for generating $show_info with its links because it deserves its own post. Suffice to say, it’s weird. Oh and $thisyear is a page variable based on the URL you visit. If you go to /this-year/2015/ it will populate as ‘2015’ and so on.

Side note. Characters actually have both a taxonomy for being dead and a lost meta for the date. That was a happy accident that allowed me to search for everyone who had died, and perhaps wasn’t currently dead. The time traveling Sara Lance is jokingly called Schroedinger’s Bisexual, since she is both alive and dead at any one given point in time.

Fix The Date

Okay! With my simple array of data, I can output the list however I want, depending on the way I ordered the query. The problem is that the query has a limited number of possible sorts, and they all have to do with the post subject, the title, not random post meta. Plus my post meta was saved in a way that wasn’t easily sortable.

To solve this, I converted the date into a Unix timestamp:

// Date(s) character died
$died_date = get_post_meta( $dead_char, 'chars_death_year', true);
$died_date_array = array();

// For each death date, create an item in an array with the unix timestamp
foreach ( $died_date as $date ) {
	$died_year = substr($date, -4);
	if ( $died_year == $thisyear ) {
		$date_parse = date_parse_from_format( 'm/d/Y' , $date);
		$died = mktime( $date_parse['hour'], $date_parse['minute'], $date_parse['second'], $date_parse['month'], $date_parse['day'], $date_parse['year'] );

I put this in the foreach( $dead_chars_query as $dead_char ) {...} section and it transformed my date from 01/01/2017 to it’s commiserate Unix timestamp.

Fix The Order

The next step was to re-order my array based on the value of the death time. I wanted to go from smallest value to largest, as Unix timestamps increase over time.

For this I used uasort to order the entire array by the date:

// Reorder all the dead to sort by DoD
uasort($death_list_array, function($a, $b) {
	return $a['died'] <=> $b['died'];

This produced my desired results. Excellent. But now I have the date in a non-human readable format!

Fix It On output

Thankfully making the date human readable wasn’t all that messy. Once I have my finished array, I output it and clean it up:

	foreach ( $death_list_array as $dead ) {
		echo '<li><a href="'.$dead['url'].'">'.$dead['name'].'</a> / '.$dead['shows'].' / '.date( 'd F', $dead['died']).' </li>';


This Year example for 2014

An ordered list of dead characters.

Reader Interactions

%d bloggers like this: