-
-
Save djrmom/11c4902f7745e75bde63e46b45e939a2 to your computer and use it in GitHub Desktop.
<?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; | |
}); |
@drjuanc - change this line:
if ( count( $parents ) !== 1 ) {
to this:
if ( count( $parents ) !== 0 ) {
Thank you for this script but I can't get it to work.
I put the script in functions.php and changed the facet-name. But what exactly is the "name of you taxonomy"? I'm using WooCommerce Product Categories. Should I leave it at product_cat?
And after that, should it just work? Many thanks in advance!
@dagaloni see my full code example below, hopefully that helps.
// index only first level children terms, no grandchildren or deeper
add_filter( 'facetwp_index_row', function( $params ) {
if ( 'course_category' == $params['facet_name'] ) {
$parents = get_ancestors( $params['term_id'], 'product_cat', 'taxonomy' );
if ( count( $parents ) !== 0 ) {
$params['facet_value'] = '';
}
}
return $params;
});
@chekle Thank you very much!
It's so strange. I can see that the script is linked correctly to my facet, but the thing is that after reindexing nothing is when if ( count( $parents ) !== 0 )
is not 0. My understanding is that when I set it to 1 the parent and only one child level is shown.
My list looks like this:
- parent
- child
- parent
- child
- second child
- child
- parent
- child
- child
- second child
I would like expand the parent and child, all second children should be collapsed.
Update: Ok, I think – even though I couldn't get the script work correctly – that this isn't the script I was looking for. It skips rows from indexing instead of collapsing it. I'm continuing my search :)
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/
Hi there.
I tried modifying it to show only the parents, no even the first child, but only parents and I can't make it work.
Any tip for that please