facetwp index parents

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php

add_filter( ‘facetwp_index_row’, function ( $params, $class ) {
if ( ‘my_facet’ == $params[‘facet_name’] ) { // change ‘my_facet’ to facet name
if ( 0 !== $params[‘depth’] ) {
// lookup parent terms
$ancestors = get_ancestors( $params[‘facet_name’], ‘name_of_taxonomy’, ‘taxonomy’ ); // ‘name_of_taxonomy’ to name of taxonomy
$starting_depth = $params[‘depth’];
foreach ( $ancestors as $ancestor ) {
$term = get_term( $ancestor, ‘name_of_taxonomy’ ); // ‘name_of_taxonomy’ to name of taxonomy
$ancester_params = $params;
$ancester_params[‘depth’] = –$starting_depth;
$ancester_params[‘facet_value’] = $term->slug;
$ancester_params[‘facet_display_value’] = $term->name;
$class->insert( $ancester_params ); // insert each value to the database
}
}
}
return $params;
}, 10, 2 );
{{/gist}}