Skip Navigation

[Resolved] Show only child terms

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by fahimS-2 1 year, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2601499

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?

Thanks in advance.

#2601917

Hi,

Thank you for contacting us and I'd be happy to assist.

To suggest the best to achieve this, I'll need to see how these taxonomy terms and this search is set up in the admin area.

Can you please share temporary admin login details, along with the link to the archive page?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2602303

Thank you for sharing the access details.

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/

#2602319

Hi,
Thanks for the answer.

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?

#2603355

For programmatic filtering of those search term options, you can use the 'get_terms_args' filter:
https://developer.wordpress.org/reference/hooks/get_terms_args/

Example code snippet:


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'.

#2603373

My issue is resolved now. Thank you!