Skip to content

Instantly share code, notes, and snippets.

@AchalJ
Last active November 3, 2021 18:18
Show Gist options
  • Select an option

  • Save AchalJ/95bc398ef287afde3cc5a760278a07ca to your computer and use it in GitHub Desktop.

Select an option

Save AchalJ/95bc398ef287afde3cc5a760278a07ca to your computer and use it in GitHub Desktop.
WooPack - Product Categories - Multi-level filters
<?php
// Please copy the code below and paste it to your current theme's functions.php
add_filter('pp_category_grid_query_args', function($args, $settings) {
if ( is_tax() || is_category() || is_tag() ) {
$current_object = get_queried_object();
$taxonomy = $current_object->taxonomy;
// Unset or remove variables.
if ( isset( $args['parent'] ) ) {
unset( $args['parent'] );
}
if ( isset( $args['exclude'] ) ) {
unset( $args['exclude'] );
}
if ( isset( $args['child_of'] ) ) {
unset( $args['child_of'] );
}
// Show only top level subcategories of current category
if ( 'default' === $settings->display_data && 'children_only' === $settings->on_tax_archive ) {
$args['parent'] = array( '', $current_object->term_id );
}
// Show all subcategories of current category.
if ( 'parent_only' === $settings->display_data && 'children_only' === $settings->on_tax_archive ) {
$args['child_of'] = $current_object->term_id;
}
// Show only children categories of top level subcategories of current category.
if ( 'children_only' === $settings->display_data && 'children_only' === $settings->on_tax_archive ) {
$args['child_of'] = $current_object->term_id;
$args['parent'] = $current_object->term_id;
$top_level_children = get_categories( $args );
$subchild_top_level = array('');
if ( ! is_wp_error( $top_level_children ) && count( $top_level_children ) ) {
foreach ( $top_level_children as $top_level_child ) {
$children = get_term_children( $top_level_child->term_id, $taxonomy );
if ( ! is_wp_error( $children ) && count( $children ) ) {
$subchild_top_level = array_merge( $subchild_top_level, $children );
}
}
}
if ( ! empty( $subchild_top_level ) ) {
unset( $args['child_of'] );
unset( $args['parent'] );
$args['include'] = $subchild_top_level;
}
}
}
return $args;
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment