Skip Navigation

[Resolved] Pre-filtering a View using custom taxonomy

This support ticket is created 3 years, 2 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)

Author
Posts
#1952515

I am trying to filter a view to display products on the following page:
hidden link

What I want to do it display products that are Division 1 Category 1 or Division 1 Category 2 ("Intrinsically Safe").

The "Division" field is a custom taxonomy with the slug "intrinsically_safe_products". This is one of many different taxonomies used to organize these products. Others include "Features", "Operating System" and "Manufacturer". I use a combination of parameters and slightly modified versions of the view to display the products on the other pages, but this page, "Intrinsically Safe", is the only one where I need to display products that fit into any one of those 2 classifications.

Not only that, when people are on that page, they need to be able to filter between C1D1, C1D2 or both, so I need to have that filter available in the Search and Pagination block.

I tried this in my functions.php file but it didn;t work:

add_filter('wpv_filter_query', 'wpv_show_prefilter_data', 99, 3);
function wpv_show_prefilter_data ( $query_args, $view_settings, $view_id ) {

if( $view_id == 3699) {

$query_args['tax_query'][] = array(
array(
'taxonomy' => 'intrinsically_safe_products',
'field' => 'slug',
'terms' => array('c1d1','c1d2'),
'operator' => 'OR',
)
);

}
return $query_args;
}

I'm not sure what to try next?

#1953433

Waqar
Supporter

Languages: English (English )

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

Hi,

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

If I understand it correctly, you have a custom taxonomy "Division" for which there are multiple terms available. But on this "Intrinsically Safe" page, you'd like the search scope to be limited to only two terms from this taxonomy: "Class I Division I" and "Class I Division II". When the page will load, by default it will show only the results limited to these two terms, and in the "Division" field in the search form too, only these two terms will be available.

If that is correct, I'll need to see exactly how this view is set up in the admin area, before I can suggest the best way to achieve this.

Can you please share temporary admin login details in reply to this message?

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

regards.
Waqar

#1962135

Hi Waqar,
I don't have the authority to add an admin user on this project so let me work on that. In the meantime I hope some more detail can help move this forward:

> If I understand it correctly, you have a custom taxonomy "Division" for which there are multiple terms available. But on this "Intrinsically Safe" page, you'd like the search scope to be limited to only two terms from this taxonomy: "Class I Division I" and "Class I Division II". When the page will load, by default it will show only the results limited to these two terms, and in the "Division" field in the search form too, only these two terms will be available.

Yes the Division label is just for site visitors, the real Taxonomy name is "Intrinsically Safe" and it has only those 2 terms.
The slugs: intrincally_safe_products, c1d1, c1d2

When the page loads it should show only products that are c1d1 OR c1d2 (or both)

> If that is correct, I'll need to see exactly how this view is set up in the admin area, before I can suggest the best way to achieve this.

Well it would be easy enough to create this view by setting up the initial content filters. The issue I am having is that users need to be able to re-filter on these same terms. In other words, the page should initially show all Intrinsically safe products but then users should be able to filter just c1d1 or c1d2 or both.

I'm having trouble because when I create the view in the standard manner, then those filters are unavailable for me to insert in the Search and Pagination section of the View Editor.

#1964079

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing these details.

I was able to make this work, using the following code on my test website:


add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
	// skip if blocks edit screen
	if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
		return $query_args;
	}
	
	// process if specific view
	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 123) ) {
		
		$found_index = 'no';

		$target_tax = "category-slug";

		$target_terms =  array('0' => 456, '1' => 789 );

		// check if filter for the target taxonomy exists
		for ($i=0; $i < (count($query_args['tax_query']) - 1) ; $i++) {
			if ( $query_args['tax_query'][$i]['taxonomy'] == $target_tax) {
				$found_index = 'yes';
			}		
		}
		//  if filter for the target taxonomy doesn't exist, add a custom one
		if( $found_index == 'no') {
			$query_args['tax_query'][] = array(
								'taxonomy'	=> $target_tax,
								'field'		=> 'id',
								'terms'		=> $target_terms,
								'operator'	=> 'IN',
								'include_children'	=> 1
								);
		}
	}
	return $query_args;
}

Note: You'll replace:

123: with actual view's ID
category-slug: with an actual slug of "Intrinsically Safe" taxonomy
456 & 789: with the actual IDs of the terms c1d1 & c1d2.

As a result, when there will be no front-end filter value set for this taxonomy filter, the view will automatically add it to limit the results to only these two terms.

And when a front-end filter value is set, that will be used.

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