Skip to content

Instantly share code, notes, and snippets.

@Nicscott01
Created January 17, 2020 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicscott01/91e3314002a2c5555c41a932a326c1ed to your computer and use it in GitHub Desktop.
Save Nicscott01/91e3314002a2c5555c41a932a326c1ed to your computer and use it in GitHub Desktop.
Filter facets before rendered
/**
* Remove taxonomy terms that aren't a parent/child of the current page
*
*
*
*/
public function facetwp_facet_render_args( $args ) {
//vdump( $args );
if ( $args['facet']['name'] !== 'industry_sector' /* && !is_tax( 'industry_sector' )*/ && !isset( $_GET['_industry_sector'] ) ) {
return $args;
}
//This is the page we're on
if ( !empty( $_GET['_industry_sector'] ) ) {
$current_page_slug = $_GET['_industry_sector'];
} else {
return $args;
}
$current_page_slug = explode( ',', $current_page_slug );
// err_dump( $current_page_slug, 'current page slug' );
//err_dump( $args, 'facetwp_facet_render_args with current page slug: ' . $current_page_slug[0] );
//Loop through and find this in the values list. Get the parent ID. Then loop through again and keep the parent and any others within that belong it it.
$keepers = [];
$is_parent = false;
$reference_value = null;
if ( !empty( $args['values'] ) ) {
foreach ( $args['values'] as $k => $value ) {
if ( $value['facet_value'] == $current_page_slug[0] ) {
$reference_value = $value;
//unset( $args['values'][$k] );
if ( $value['parent_id'] == 0 ) {
$is_parent = true;
//$keepers[] = $value;
}
}
}
foreach ( $args['values'] as $k => $value ) {
if( $is_parent ) {
if ( $reference_value['term_id'] == $value['parent_id'] || $reference_value['term_id'] == $value['term_id'] ) {
$keepers[] = $value;
} else {
unset( $args['values'][$k] );
}
} else {
if ( $reference_value['parent_id'] == $value['parent_id'] || $reference_value['parent_id'] == $value['term_id'] ) {
$keepers[] = $value;
} else {
unset( $args['values'][$k] );
}
}
}
//err_dump( $keepers, 'My keepers for current_page_slug: ' . $current_page_slug[0] );
$args['values'] = $keepers; //array_values( $args['values'] );
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment