facetwp_index_row

Overview

Modify individual values before they’re saved to FacetWP’s index table.

Parameters

  • $params | array | An associative array of data to be indexed (see below)
  • $class | object | The indexer class

$params = array(
    'post_id' => 123,
    'facet_name' => 'my_facet',
    'facet_source' => 'tax/category',
    'facet_value' => '45',
    'facet_display_value' => 'My Test Category',
    'term_id' => 0,
    'parent_id' => 0,
    'depth' => 0,
    'variation_id' => 0
);

Note: the $params array corresponds to columns in the facetwp_index table

Usage

Index only top-level terms (depth = 0):

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'categories' == $params['facet_name'] ) {
        if ( 0 < $params['depth'] ) {
            $params['facet_value'] = ''; // don't index this row
        }
    }
    return $params;
}, 10, 2 );

Index only child terms (depth = 1):

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'categories' == $params['facet_name'] ) {
        if ( 1  !== $params['depth'] ) {
            $params['facet_value'] = ''; // don't index this row
        }
    }
    return $params;
}, 10, 2 );

Index each value within a serialized array:

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'your_serialized_field_name' == $params['facet_name'] ) {
        $values = maybe_unserialize( $params['facet_value'] );
        if ( is_array( $values ) && ! empty( $values ) ) {
            foreach ( $values as $value ) {
                $params['facet_value'] = $value;
                $params['facet_display_value'] = $value;
                $class->insert( $params ); // insert new value to the database
            }
            $params['facet_value'] = ''; // skip original row
        }
    }
    return $params;
}, 10, 2 );

More examples

Other Notes

  • facet_value is used for the URL / permalink
  • facet_display_value is used as the label (front-end)
  • To skip the current row, set facet_value to an empty string.

See Also

facetwp_indexer_row_data

How to include / exclude facet choices