Skip Navigation

[Resolved] Custom taxonomy dynamically choose all filter

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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 2 replies, has 2 voices.

Last updated by Waqar 3 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1613757

I have this custom taxonomy (SPECIAL PRODUCT) and I want to show in a view all items that have any category (Books, Radios, TV) chosen under this taxonomy. But I need to allow client to add or remove those categories.

So I would need a dynamic filter. For some reason I can't figure out how to do this with query filters. With "Any of the following" or similar, I have to activate the categories manually. There's no "wildcard" option. Or is there, where?

#1614835

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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

To suggest the best way to achieve this, I'll need to see how this view is set up in the admin area.

Can you please share temporary admin login details along with a link to a page where this view can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1617211

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for sharing these details.

To include a taxonomy filter in your view, that checks for all the available terms, you can remove the taxonomy filter added in the view's "Query Filter" section and instead use the "wpv_filter_query" filter to programmatically include it:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )

Example:


add_filter( 'wpv_filter_query', 'filter_for_taxonomy_custom_fn', 1000 , 3 );
function filter_for_taxonomy_custom_fn( $query_args, $view_settings ) {

	// if specific view
	if ( ( !is_admin() && isset($view_settings['view_id'] ) ) && ( $view_settings['view_id'] == 1234 ) ) 
	{
		// slug of the target taxonomy
		$taxonomy_slug = "book-type";

		// get the list of all the terms from that taxonomy
		$taxonomies = get_terms( array(
			'taxonomy' => $taxonomy_slug,
			'hide_empty' => false
		) );

		// store those term IDs in an array
		foreach( $taxonomies as $taxonomy ) {
			$terms[] = $taxonomy->term_id; 
		}

		// if some term IDs are available, include a taxonomy filter in the view's query
		if(!empty($terms)) {
			$term_array[] = array(
								'taxonomy' => $taxonomy_slug,
								'field' => 'id',
								'terms' => $terms,
								'operator' => 'IN',
								'include_children' => 1,
								 );

			$query_args['tax_query'] = $term_array;
			$query_args['tax_query']['relation'] = 'AND';
		}
		
	}

	return $query_args;
}

Notes:

1. The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

2. You'll replace "1234" with the actual View's ID and the "book-type" with the actual taxonomy's slug.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.