<?php
add_filter( 'facetwp_index_row', function( $params ) {
if ( 'my_facet' == $params['facet_name'] ) { // 'my_facet' should be replaced with your facet name
$parents = get_ancestors( $params['term_id'], 'category', 'taxonomy' ); // change 'category' to name of your taxonomy
if ( ! in_array( '50', $parents ) ) { // change 50 to term_id of your top level term
$params[ 'facet_value' ] = '';
} elseif ( 2 > count( $parents ) ) { // adjust 2 if the parent term is not a top level term, ie 3 for second level term as the parent
$params[ 'parent_id' ] = 0;
$params[ 'depth' ] = 0;
} else {
$params[ 'depth' ] = $params[ 'depth' ] - 1;
}
}
return $params;
});