Forums › Forums › Search & Filter Pro › Show ONLY current category’s subcategories
Tagged: category
- This topic has 20 replies, 3 voices, and was last updated 3 years, 9 months ago by Ross.
-
Ross Moderator(Private) February 1, 2021 at 1:33 pm #274760
Hi Danny
Writing something like that is a little out of scope of support (should require custom development) however I’ve gone ahead and bashed something toegether for you anyway (I won’t be able to customise this for you):
function sf_filter_options_sub_terms( $input_object, $sfid ) { $filter_name = '_sft_category'; $taxonomy_name = 'category'; $parent_term_id = 2; // you need to figure out the ID of the parent if ( $input_object['name'] !== $filter_name ) { return $input_object; } if ( ! isset( $input_object['options'] ) ) { return $input_object; } if ( ! is_array( $input_object['options'] ) ) { return $input_object; } // ask WP for the child terms $child_terms = get_terms( $taxonomy_name, array( 'parent' => $parent_term_id, 'orderby' => 'slug', 'hide_empty' => false ) ); // init the first option $default_option = new StdClass(); $default_option->value = ''; $default_option->label = 'Choose a Category'; // setup the new options array $child_options = array( $default_option ); // grab a reference to the search form global $searchandfilter; $search_form = $searchandfilter->get( $sfid ); // loop through child terms foreach ( $child_terms as $child_term ) { // add back in the dynamic count $term_count = $search_form->get_count_var( $filter_name, $child_term->slug ); //create a new option for the term $new_option = new StdClass(); $new_option->value = $child_term->slug; $new_option->label = $child_term->name; $new_option->count = $term_count; array_push( $child_options, $new_option ); } // replace the old options with the new ones $input_object['options'] = $child_options; return $input_object; } add_filter('sf_input_object_pre', 'sf_filter_options_sub_terms', 10, 2);
What you need to do is change the 3 variables at the top to match your own:
$filter_name = '_sft_category'; $taxonomy_name = 'category'; $parent_term_id = 2
The first
$filter_name
is the name of your field in our plugin, which will be –_sft_product_cat
The second is the actual taxonomy internal nameproduct_cat
And the third is the WP ID of the parent Product category (you can usually figure this out from WP admin by editing the term, and checking the URL)Once you replace those, this code will replace the options, with the child options of
$parent_term_id
.Let me know how you get on.
Thanks
-
AuthorPosts