Algolia for Wordpress:在创建过滤器构面小部件时,如何对多个属性的值求和?
Algolia for Wordpress: In creating a filter facet widget, how do I sum values from multiple attributes?
我在 Wordpress 中使用 Algolia 搜索,我正在尝试创建一个 facet,允许用户根据数字范围过滤结果。问题是我需要获取多个属性的总和以进行比较。
例如,假设我有以下数据:
{
"paid_staff_male": 24,
"paid_staff_female": 21,
"paid_staff_other": 2
}
我如何创建一个 facet 小部件,允许用户使用最小到最大滑块,或者最小和最大的两个输入来根据总付费员工过滤结果?
因此,在上面的示例中,post 总共有 47 名带薪员工。我将如何生成一个看起来像这样的 facet/filter:
Paid Staff
0 <--[40]---------[500]--------------> 1000
...或者,这个:
Paid Staff
Min Max
[__40___] - [__500__]
这是我的 instantsearch.js 文件中目前的方面:
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>'
}
})
);
在"attributeName"下,我需要return"paid_staff_male"、"paid_staff_female"和"paid_staff_other"的总和。
我需要这个:
attributeName: sum('paid_staff_male', "paid_staff_female", "paid_staff_other"),
任何建议将不胜感激:)
instantsearch.js 价格范围小部件
关于用户界面,您可能希望使用以下 instantsearch.js 小部件中的一两个:
正在准备过滤数据
Algolia 在索引时计算很多东西,以确保查询时的速度尽可能快。
在您的情况下,为了能够对 3 个不同的属性使用唯一的过滤器,您应该在应用程序级别进行计算并将该计算的结果作为记录的新属性部分发送。
如何使用 WordPress 的 Algolia 插件发送自定义属性
这里有一些代码可以帮助您将总和作为一个新属性推送,并使其读取以进行分面:
<?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' );
我在 Wordpress 中使用 Algolia 搜索,我正在尝试创建一个 facet,允许用户根据数字范围过滤结果。问题是我需要获取多个属性的总和以进行比较。
例如,假设我有以下数据:
{
"paid_staff_male": 24,
"paid_staff_female": 21,
"paid_staff_other": 2
}
我如何创建一个 facet 小部件,允许用户使用最小到最大滑块,或者最小和最大的两个输入来根据总付费员工过滤结果?
因此,在上面的示例中,post 总共有 47 名带薪员工。我将如何生成一个看起来像这样的 facet/filter:
Paid Staff
0 <--[40]---------[500]--------------> 1000
...或者,这个:
Paid Staff
Min Max
[__40___] - [__500__]
这是我的 instantsearch.js 文件中目前的方面:
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>'
}
})
);
在"attributeName"下,我需要return"paid_staff_male"、"paid_staff_female"和"paid_staff_other"的总和。
我需要这个:
attributeName: sum('paid_staff_male', "paid_staff_female", "paid_staff_other"),
任何建议将不胜感激:)
instantsearch.js 价格范围小部件
关于用户界面,您可能希望使用以下 instantsearch.js 小部件中的一两个:
正在准备过滤数据
Algolia 在索引时计算很多东西,以确保查询时的速度尽可能快。
在您的情况下,为了能够对 3 个不同的属性使用唯一的过滤器,您应该在应用程序级别进行计算并将该计算的结果作为记录的新属性部分发送。
如何使用 WordPress 的 Algolia 插件发送自定义属性
这里有一些代码可以帮助您将总和作为一个新属性推送,并使其读取以进行分面:
<?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' );