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

facetwp_index_row

Overview

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

Parameters

// The keys correspond with columns in the `facetwp_index` table
$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
);

Usage

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

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

Index a serialized array of values:

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'YOUR_FACET_NAME_HERE' == $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 );

Other Notes

See Also

facetwp_indexer_row_data

How to include / exclude facet choices