I have a toolset archive where I have added a taxonomy search. The taxonomy has some terms. 4 of the terms (five, six, seven, eight) has child terms. I want to show only child terms of five and six. How can I do that?
As there is no built-in feature to achieve filtering of term options in the search, this will require some custom script code.
For example here is a code snippet that cycles through the options and removes the ones which don't have 'five-' or 'six-' in the value:
jQuery( document ).on( 'ready js_event_wpv_pagination_completed js_event_wpv_parametric_search_form_updated js_event_wpv_parametric_search_results_updated', function( event, data ) {
jQuery('.wpv-custom-search-filter input[name="wpv-saas-feature[]"]').each(function () {
var tHis = jQuery(this);
if (tHis.length) {
var selText = tHis.val();
if ( (selText.indexOf('five-') >= 0) || (selText.indexOf('six-') >= 0) ) {}
else { tHis.parents('.form-check').remove(); }
}
});
});
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors: https://toolset.com/contractors/
The problem is that the site that I gave access to is my test site. So the terms it has are for testing purpose. I replicated the problem on this test site. So the terms on my real site, doesn't start with 'five-' or 'six-'. Those are random strings on my real site.
Isn't there any way to remove the terms from backend by using 'pre-get-terms' hook. I saw that it has filtering options. Actually I tried it but couldn't achieve it.
Is it possible to solve this problem with 'pre-get-terms' hook?
function custom_get_terms_args( $args, $taxonomies ) {
// execute only if not admin area and page is the post type archive for 'book' and the taxonomy query is for the 'book-category'
if ( (! is_admin()) && (is_post_type_archive('book')) && ($taxonomies[0] == 'book-category' ) ) {
// include only the terms with these IDs
$args['include'] = array(2,4, 6);
}
return $args;
}
add_filter( 'get_terms_args', 'custom_get_terms_args', 10, 2 );
The above code snippet will work on the 'book' type post archive page, for the query of the 'book-category' terms, to limit only to terms with ID '2', '4' & '6'.