Skip to content

Instantly share code, notes, and snippets.

@cparkinson
Created September 8, 2022 20:40
Show Gist options
  • Save cparkinson/19a78658f45d43a1ff47d5b6ff25e298 to your computer and use it in GitHub Desktop.
Save cparkinson/19a78658f45d43a1ff47d5b6ff25e298 to your computer and use it in GitHub Desktop.
FacetWP when indexing all posts, create an index row for a particular term
/**
*
* When indexing, for every post, for the location 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