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 = [
'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 examples
Index only specific term levels
Index only top-level terms (depth = 0):
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'my_facet_name' == $params['facet_name'] ) { // replace 'my_facet_name' with the name of your facet
if ( $params['depth'] > 0 ) {
$params['facet_value'] = ''; // don't index this row
}
}
return $params;
}, 10, 2 );
Index only the first-level direct child terms (depth = 1):
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'my_facet_name' == $params['facet_name'] ) { // replace 'my_facet_name' with the name of your facet
if ( $params['depth'] !== 1 ) {
$params['facet_value'] = ''; // don't index this row
}
}
return $params;
}, 10, 2 );
Index only the top-level terms (depth = 0) and their direct children (depth = 1):
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'my_facet_name' == $params['facet_name'] ) { // replace 'my_facet_name' with the name of your facet
if ( $params['depth'] > 1 ) {
$params['facet_value'] = ''; // don't index this row
}
}
return $params;
}, 10, 2 );
Index a serialized array
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 );
Other notes
facet_value
is used for the URL / permalinkfacet_display_value
is used as the label (front-end)- To skip the current row, set
facet_value
to an empty string.