Skip to content

Instantly share code, notes, and snippets.

@djrmom
Created November 21, 2017 18:46
Show Gist options
  • Save djrmom/d43191d1709ce00804b76a14baf9fc85 to your computer and use it in GitHub Desktop.
Save djrmom/d43191d1709ce00804b76a14baf9fc85 to your computer and use it in GitHub Desktop.
facetwp index parents
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment