Last active
August 25, 2020 16:01
-
-
Save djrmom/5b0c47087ab9b2ad669171a22b0cfb1d to your computer and use it in GitHub Desktop.
facetwp index only direct children of a parent term
This file contains hidden or 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 | |
/** | |
** 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment