facetwp_facet_render_args
Overview
This filter lets you modify the data passed to a facet’s render() method. This is useful if you need to alter the selected values or translate facet choices.
Parameters
- $args | array | An associative array of facet render arguments (see below)
$args = [
'facet' => [
'label' => 'Model Year',
'name' => 'model_year',
'type' => 'checkboxes',
...
],
'where_clause' => 'AND post_id IN (18670,18671,18672,18673,18674)',
'selected_values' => [
'2014',
'2015',
'2016'
]
];
Some facet types also have $args['values']
, depending on whether the facet type has a load_values() method.
Usage
Modify the selected values for a specific facet (e.g. model_year
):
add_filter( 'facetwp_facet_render_args', function( $args ) {
if ( 'model_year' == $args['facet']['name'] ) {
$args['selected_values'] = [ '2015' ];
}
return $args;
});
Manually translate choices within the facet named “country”:
add_filter( 'facetwp_facet_render_args', function( $args ) {
if ( 'country' == $args['facet']['name'] ) {
$translations = [
'Spain' => __( 'Spain', 'fwp' ),
'United States' => __( 'United States', 'fwp' ),
'France' => __( 'France', 'fwp' )
];
if ( ! empty( $args['values'] ) ) {
foreach ( $args['values'] as $key => $val ) {
$display_value = $val['facet_display_value'];
if ( isset( $translations[ $display_value ] ) ) {
$args['values'][ $key ]['facet_display_value'] = $translations[ $display_value ];
}
}
}
}
return $args;
});