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)
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
$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
):
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
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”:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
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; });