Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
facetwp match facet term choices to hierarchy of a taxonomy archive page
<?php
/** on a taxonomy term page remove terms from facet that are not the same or a sub category of that term
* example is for woocommerce product categories
*/
add_filter( 'facetwp_facet_render_args', function( $args ) {
if ( 'product_categories' == $args['facet']['name'] && is_tax( 'product_cat' ) ) {
$current_term = get_queried_object_id();
if ( ! empty( $args['values'] ) ) {
foreach ( $args['values'] as $key => $val ) {
$term_id = $val['term_id'];
// this keeps sub-terms of the current term archive page
if ( $val['parent_id'] && in_array( $current_term, get_ancestors( $val['term_id'], 'product_cat', 'taxonomy' ) ) ) {
continue;
// this keeps term for current archive page, delete this else if to if not wanting the current term also
} else if ( $current_term == $val['term_id'] ) {
continue;
// this removes non-matching terms from the choices for the facet.
} else {
unset( $args['values'][$key] );
}
}
$args['values'] = array_values( $args['values'] );
}
}
return $args;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.