Jim Kulakowski
Jim Kulakowski

Reputation: 67

Algolia for Wordpress: In creating a filter facet widget, how do I sum values from multiple attributes?

I'm using Algolia search within Wordpress and I'm attempting to create a facet that will allow the user to filter results based on a numerical range. The issue is that I need to get the sum of a number of attributes for comparison.

For instance, let's say I have the following data:

{
  "paid_staff_male": 24,
  "paid_staff_female": 21,
  "paid_staff_other": 2
}

How do I create a facet widget that allows a user to either use a min to max slider, or two inputs for min and max to filter results based on total paid staff?

So, in the above example, this post has 47 total paid staff. How would I produce a facet/filter that looks something like this:

Paid Staff
0 <--[40]---------[500]--------------> 1000

...or, this:

Paid Staff
   Min        Max
[__40___] - [__500__]

Here is how my facets currently look in my instantsearch.js file:

search.addWidget(
    instantsearch.widgets.menu({
        container: '#some-facet',
        attributeName: 'some_attribute',
        sortBy: ['isRefined:desc', 'count:desc', 'name:asc'],
        templates: {
            header: '<h3 class="widgettitle">Facet Title</h3>'
        }
    })
);

Under "attributeName", I need to return the sum of "paid_staff_male", "paid_staff_female", and "paid_staff_other".

I need this:

attributeName: sum('paid_staff_male', "paid_staff_female", "paid_staff_other"),

Any advice would be greatly appreciated :)

Upvotes: 0

Views: 533

Answers (1)

rayrutjes
rayrutjes

Reputation: 798

instantsearch.js price ranges widgets

Regarding the user interface, you will probably want to use one or 2 of following instantsearch.js widgets:

Preparing data for filtering

Algolia computes a lot of things at indexing time to ensure the speed at query time is as good as possible.

In your case, to be able to have a unique filter for 3 different attributes, you should do the computation at your application level and send the result of that computation as a new attribute part of the record.

How to send custom attributes with the Algolia plugin for WordPress

Here is some code that will help you push the sum as a new attribute and also make it reading for faceting:

<?php
/**
 * Compute and push the sum as part of the post records.
 *
 * @param array   $shared_attributes
 * @param WP_Post $post
 *
 * @return array
 */
function custom_shared_attributes( array $shared_attributes, WP_Post $post) {
    $shared_attributes['paid_staff_sum'] = (float) get_paid_staff_sum( $post );

    return $shared_attributes;
}
add_filter( 'algolia_post_shared_attributes', 'custom_shared_attributes', 10, 2 );
add_filter( 'algolia_searchable_post_shared_attributes', 'custom_shared_attributes', 10, 2 );

function get_paid_staff_sum( WP_Post $post ) {
    // This is just an example, you should adjust it to match your way
    // of computing the sum.
    return get_post_meta( $post->ID, 'paid_staff_male', true )
    + get_post_meta( $post->ID, 'paid_staff_female', true )
    + get_post_meta( $post->ID, 'paid_staff_other', true );
}

/**
 * Make sure you can facet on the newly available attribute.
 * 
 * @param array $settings
 *
 * @return array
 */
function custom_attributes_for_faceting( array $settings )  {

    $settings['attributesForFaceting'][] = 'paid_staff_sum';

    return $settings;
}

add_filter( 'algolia_posts_index_settings', 'custom_attributes_for_faceting' );
add_filter( 'algolia_searchable_posts_index_settings', 'custom_attributes_for_faceting' );

Upvotes: 2

Related Questions