I’ve talked about this before in category statistics, but in order to get the data from a simple array into a Chart.js consumable one, we have to rebuild the array.

All Arrays are not Equal

In order to save the data in a way I could use and reuse, I had to aim at the lowest common denominator. But also I had to save the arrays at a per show basis, which is not the same as what I was going to need to output.

Instead of just outputting the averages for the show, I needed to combine all this into a ‘by nation’ statistic. That is, I needed to get a list of all shows that were associated with a taxonomy value for that country (easy) and combine all their arrays (not quite easy) and order the data in a way that would make sense (not easy).

So again we start with understanding the array. Here’s a show that happens to air in Argentina:

    [cisgender]    => 2
    [trans-woman]  => 0
    [trans-man]    => 0
    [non-binary]   => 0
    [gender-fluid] => 0
    [gender-queer] => 0
    [agender]      => 0

This is the data for one show. Argentina has 2, oddly both with the same stats breakdown by gender identity. What I need to do is loop through both those shows and add the arrays to be this:

    [cisgender]    => 4
    [trans-woman]  => 0
    [trans-man]    => 0
    [non-binary]   => 0
    [gender-fluid] => 0
    [gender-queer] => 0
    [agender]      => 0

Get the Base Arrays

Just like before, we make an array of the base data as we have it in the gender, sexuality, and romantic orientations. In this case, we’re adding in a query to change the order to be largest to smallest overall from the taxonomy. While this may not be true for all nations in the future, it is today:

$taxonomy = get_terms( 'lez_nations' );
foreach ( $taxonomy as $the_tax ) {

I need to pause here. Everything from here out goes in that foreach. We’re going to be looping for each nation in the list of nations. Now… I re-use this code for multiple taxonomies, so lez_nations is actually lez_' . $data and it dynamically changes based on how I call this function.

On we go!

	$characters = 0;
	$shows      = 0;
	// Create a massive array of all the character terms we care about...
	$valid_char_data = array( 
		'gender'    => 'lez_gender',
		'sexuality' => 'lez_sexuality',
		'romantic'  => 'lez_romantic',

	if ( isset( $subdata ) && !empty( $subdata ) ) {
		$char_data = array();
		$terms     = get_terms( $valid_char_data[ $subdata ], array( 'orderby' => 'count', 'order' => 'DESC' ) );

		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
			foreach ( $terms as $term ) {
				$char_data[ $term->slug ] = 0;

Now that we have those base arrays, again set to zero,

By the way, $subdata and $data are parameters sent to the function that runs this. $subdata is for the taxonomy we’re calculating (sexuality etc) and $data is for the overall taxonomy (Nations or perhaps Stations or genres – we use a lot of those).

This gets us started.

Queery the Posts

Next we need a WP_Query of all the posts in the taxonomy.

	$count = wp_count_posts( 'post_type_shows' )->publish;
	$queery = new WP_Query ( array(
		'post_type'              => 'post_type_shows',
		'posts_per_page'         => $count,
		'post_status'            => array( 'publish' ),
		'tax_query'              => array( array(
			'taxonomy' => 'lez_' . $data,
			'field'    => 'slug',
			'terms'    => $the_tax->slug,
			'operator' => '=',
	) );

Remember, this is still within that foreach above. And once we have the posts, let’s query all the shows:

	if ( $queery->have_posts() ) {
		foreach( $queery->posts as $show ) {

			// Get all the crazy arrays
			$gender = get_post_meta( $show->ID, 'lezshows_char_gender' );
			if ( isset( $subdata ) ) { 
				$dataset = get_post_meta( $show->ID, 'lezshows_char_' . $subdata );

			// Add the character counts
			foreach( array_shift( $gender ) as $this_gender => $count ) {
				$characters += $count;

			if ( !empty( $dataset ) ) {
				foreach( array_shift( $dataset ) as $this_data => $count ) {
					$char_data[ $this_data ] += $count;


The weird section you see, // Add the character counts is there because every character has a gender, but not everyone has a sexuality or romantic orientation. Because of that, I decided it was safest to use that as my baseline count.

The second section that checks if ( !empty( $dataset ) ) {...} is what adds things up for the array.

Speaking of…

Output the New Array

Once I have those counts, I generate different arrays depending on what I’m outputting. The basic barchart is different from a percentage, which is different from the stacked bar.

	// Determine what kind of array we need to show...
	switch( $format ) {
		case 'barchart':
			$array[] = array (
				'name'  => $the_tax->name,
				'count' => $shows,
		case 'percentage':
			$array = self::taxonomy( 'post_type_shows', 'lez_' . $data );
		case 'count':
			$array = count( $taxonomy );
		case 'stackedbar':
			$array[$the_tax->slug] = array(
				'name'       => $the_tax->name,
				'count'      => $shows,
				'characters' => $characters,
				'dataset'    => $char_data,

And all of this is so I could get that silly stacked bar, which will have the count of total characters, shows, and the data.


Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: