X Features Cars Demo Power Plants Demo State Parks Demo Site Showcase Docs Tutorials Add-ons Buy

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 programmatically alter the selected values.

Parameters

$args = array(
    'facet' => array(
        'label' => 'Model Year',
        'name' => 'model_year',
        'type' => 'checkboxes',
        ...
    ),
    'where_clause' => 'AND post_id IN (18670,18671,18672,18673,18674)',
    'selected_values' => array(
        '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'] = array( '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 = array(
            '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;
});