Created
May 13, 2020 16:22
-
-
Save cparkinson/69325c78bed3335739ee308b1893c7a5 to your computer and use it in GitHub Desktop.
FacetWP - automatically index all child terms when a parent taxonomy term is checked
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
/** | |
* Filter to customize FacetWP, should be pasted in FacetWP's "Custom Hooks" plugin | |
* | |
* Made for a "location" FacetWP autocomplete filter that uses a taxonomy "location" | |
* consisting of parent county terms and child city terms | |
* | |
* When indexing, for every post, for the location taxonomy filter, | |
* Check to see if it has any location taxonomy set | |
* Check to see if selected location taxonomy has children (i.e. selected county has cities) | |
* Create a new index row for each child city | |
* | |
* So that Admin Panel editors can check only county for content | |
* But front-end users will be able to type city into autocomplete and find parent county content | |
*/ | |
add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) { | |
if ( 'location_filter' == $params['facet']['name'] ) { | |
$post_id = (int) $params['defaults']['post_id']; | |
$locations = get_the_terms( $post_id, 'location' ); | |
if ( !empty( $locations ) ) { | |
foreach ( $locations as $location ) { | |
$child_term_ids = get_term_children( $location->term_id, 'location' ); | |
if ( !empty( $child_term_ids ) ){ | |
foreach ($child_term_ids as $child_term_id){ | |
$term = get_term_by('id', $child_term_id, 'location'); | |
$new_row = $params['defaults']; | |
$new_row['facet_value'] = $term->slug; | |
$new_row['facet_display_value'] = $term->name; | |
$new_row['term_id'] = $term->term_id; | |
$new_row['parent_id'] = $term->parent; | |
$new_row['depth'] = '1'; | |
$rows[] = $new_row; | |
} | |
} | |
}//foreach location term | |
}//if location taxonomy assigned | |
} | |
return $rows; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment