Jim*_*ski 0 wordpress filter facet algolia
我在Wordpress中使用Algolia搜索,我正在尝试创建一个允许用户根据数值范围过滤结果的方面.问题是我需要获得许多属性的总和以进行比较.
例如,假设我有以下数据:
{
"paid_staff_male": 24,
"paid_staff_female": 21,
"paid_staff_other": 2
}
Run Code Online (Sandbox Code Playgroud)
如何创建一个facet小部件,允许用户使用最小到最大滑块,或者使用最小和最大两个输入来根据总付费人员过滤结果?
因此,在上面的例子中,这篇文章共有47名付薪员工.我如何生成一个如下所示的构面/过滤器:
Paid Staff
0 <--[40]---------[500]--------------> 1000
Run Code Online (Sandbox Code Playgroud)
...或这个:
Paid Staff
Min Max
[__40___] - [__500__]
Run Code Online (Sandbox Code Playgroud)
以下是我的facets当前在我的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>'
}
})
);
Run Code Online (Sandbox Code Playgroud)
在"attributeName"下,我需要返回"paid_staff_male","paid_staff_female"和"paid_staff_other"的总和.
我需要这个:
attributeName: sum('paid_staff_male', "paid_staff_female", "paid_staff_other"),
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激 :)
instantsearch.js价格范围小部件
关于用户界面,您可能希望使用以下一个或两个instantsearch.js小部件:
准备过滤数据
Algolia在索引时计算了很多东西,以确保查询时的速度尽可能好.
在您的情况下,为了能够为3个不同的属性设置唯一的过滤器,您应该在应用程序级别进行计算,并将该计算的结果作为记录的新属性部分发送.
如何使用Algolia插件为WordPress发送自定义属性
下面是一些代码,它们可以帮助您将总和作为新属性推送,并使其读取分面:
<?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' );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
339 次 |
| 最近记录: |