facetwp index only direct children of a parent term

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php
/**
** filter on facetwp_index_row to index only direct children of a parent term
** this is for use with Parent term setting which by default indexes child terms, and grand child terms, etc
** If you want to have the index include posts of child/grandchild, etc of terms, you need to set Hierarchical to yes
**/

add_filter( ‘facetwp_index_row’, function( $params, $class ) {
if ( in_array( $params[‘facet_name’], array( ‘my_facet’, ‘my_other_facet’ ) ) ) { // create an array of facet names to check against
$parent = $class->facet[‘parent_term’]; // this gives the parent term from the facet’s settings
if ( $parent != $params[‘parent_id’] ) { // $params[‘parent_id’] is the parent id of the term being indexed
$params[‘facet_value’] = ”; // skip indexing
}
}
return $params;
}, 10, 2 );
{{/gist}}