Last active
March 29, 2022 13:49
-
-
Save djrmom/11c4902f7745e75bde63e46b45e939a2 to your computer and use it in GitHub Desktop.
facetwp index only 1st level children in taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** index only first level children terms, no grandchildren or deeper **/ | |
add_filter( 'facetwp_index_row', function( $params ) { | |
if ( 'product_categories' == $params['facet_name'] ) { //change 'product_categories' to name of your facet | |
$parents = get_ancestors( $params['term_id'], 'product_cat', 'taxonomy' ); // change 'product_cat' to name of your taxonomy | |
if ( count( $parents ) !== 1 ) { // adjust comparison as needed for different child levels | |
$params['facet_value'] = ''; // skip indexing | |
} | |
} | |
return $params; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The original function is old. Its actually better to check
$params['depth']
rather than lookup the term's parents with get_ancestors. See example - https://gist.facetwp.com/gist/gist-89af85b742c856aa2b3f193e019edd5b/