That’s a pun because I’m using FacetWP y’all.

As you probably know by now, I have a site that has a lot of weird data. And one of the problems I ran into with FacetWP and my site was that I saved data in a serialized manner.

Simple Arrays

Since characters can have multiple actors, the data is saved in a serialized array like this: a:1:{i:0;s:13:"Lucy Lawless";}

I want to be able to search for all the characters Lucy Lawless plays, even if someone else also played the role, so to do that I need to tell FacetWP to save the data twice, an entry for each actor. To do that, I use this code:

add_filter( 'facetwp_index_row', 'filter_facetwp_index_row', 10, 2 );
function filter_facetwp_index_row( $params, $class ) {	
	// Actors
	// Saves one value for each actor
	if ( 'char_actors' == $params['facet_name'] ) {
		$values = (array) $params['facet_value'];
		foreach ( $values as $val ) {
			$params['facet_value'] = $val;
			$params['facet_display_value'] = $val;
			$class->insert( $params );
		return false; // skip default indexing
	return $params;

That saves two entries in the FacetWP table like this:

An example of two actors for one character

Complex Arrays

Buuuuut I also have a complex array where I list a show’s airdates: a:2:{s:5:"start";s:4:"1994";s:6:"finish";s:4:"2009";}

Now that doesn’t look too weird, I know, but the problem is I wanted to be able to compare the start and end dates, so you could get a list of, say, all shows that were on air between 1950 and 1960 (two, by the way). In order to do that, I had to break the array apart into not only two values, but two separate sources!

In order to make that work, I do this:

// Airdates
// Splits array value into two sources
if ( 'show_airdates' == $params['facet_name'] ) {
	$values = (array) $params['facet_value'];

	$start = ( isset( $values['start'] ) )? $values['start'] : '';
	$end   = ( isset( $values['finish'] ) )? $values['finish'] : date( 'Y' );

	$params['facet_value']         = $start;
	$params['facet_display_value'] = $start;
	$class->insert( $params );

	$params['facet_source']        = 'cf/lezshows_airdates_end';
	$params['facet_name']          = 'show_airdates_end';
	$params['facet_value']         = $end;
	$params['facet_display_value'] = $end;
	$class->insert( $params );
	return false; // skip default indexing

That gives me two database entries like so:

An example of two values in two separate sources

The reason this is done is because I have a facet that compares the datasets for lezshow_airdates_end with lezshow_airdates and if the numbers are between them, that’s what it shows.

And this works because of this filter:

// Filter Facet sources
add_filter( 'facetwp_facet_sources', function( $sources ) {
    $sources['custom_fields']['choices']['cf/lezshows_airdates_end'] = 'Airdates End';
    return $sources;

That creates a new custom field based on the values in cf/lezshows_airdates_end so I can compare between the two. And with a snazzy slider, I can do this:

Aired Between ... as a slider

Reader Interactions

%d bloggers like this: