Skip Navigation

[Resolved] Allow frontend user to change "any" to "all"

This support ticket is created 6 years, 1 month 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 6 replies, has 2 voices.

Last updated by Ido Angel 6 years ago.

Assisted by: Waqar.

Author
Posts
#1136749

Hey,
Is there a way I can allow users to switch between searching "any" or "all" when filtering custom fields and taxonomies?
For example, show writers (CPT) who write either "poetry" or "prose" (taxonomy) but bot writers who write both / then switch to only show writers who write both prose and poetry.

Thx!

#1137409

Hi Ido,

Thanks for asking! I'd be happy to help.

There is no direct query filter available to allow users to adjust the taxonomy query filtration type, but you can use the following workaround:

1. In your view's "Search and Pagination" section, please add the following code to show a drop down, to select the taxonomy query type:


<div class="form-group">
	<label>[wpml-string context="wpv-views"]Query type[/wpml-string]</label>
	<select id="wpv_control_select_query_type" name="custom_query_type" class="js-wpv-filter-trigger form-control">
		<option value="IN">any of the selected categories</option>
		<option value="NOT IN">not one of the selected categories</option>
		<option value="AND">all of the selected categories</option>
	</select>
</div>

Make sure to allow your taxonomy filter to choose multiple terms (through multiple checkboxes) and show this custom field after that. Also, select "any of the values" in the taxonomy filter settings, as a default filtering option, as that is the first option in the select field too.
( screenshot: hidden link )

2. Next, using "wpv_filter_query" hook, you can filter the query of the view, based on the "custom_query_type" value, passed through this custom filtering field:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )


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

	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 123) ) {

		if (  (!empty( $query_args['tax_query'])) && (!empty( $_GET['custom_query_type'])) ) {
			$query_args['tax_query'][0]['operator'] = $_GET['custom_query_type'];
		}
	}
	return $query_args;
}

Note: replace 123 with the actual ID of your view.

This should do the trick. Please let me know how it goes and if you need any further assistance around this.

regards,
Waqar

#1137573

hey,
thanks!
i did it, but what i get now is that the taxonomy serch filter itself is empty - there are no terms to choose from.
also note that when i tried editing the function.php file directly, i got an error. i only managed to edit it on my desktop and then ftp it to the server.
another question: can i do this with checkboxes, or onlu "select multiple"?
thanks!
ido

#1138019

Hi Ido,

Thanks for writing back.

If some taxonomy terms are available and the taxonomy search filter is still empty, I'll suggest removing its existing field from "Search and Pagination" section and its taxonomy filter from the "Query Filter" settings and then add them again.

Here is a screenshot of "Search and Pagination" section from my view:
hidden link

And this is how the taxonomy filter was set:
hidden link

On the front-end, these filters show, like this:
hidden link

In my example, I've used a select field for the custom query type parameter, but you can replace it with a field of different type too (e.g. checkboxes, radio buttons etc). The objective is to let visitor select any one of the values from "IN", "NOT IN" & "AND" get that passed on with the search form.

In case the issue still persists, I’ll need temporary access (WP-Admin and FTP) to the website, preferably a development or cloned website, where the View that you're working with can be seen.

This access will allow me to offer better help and check if some configurations or code might need to be changed.

Your next answer will be private which means only you and our support team will have access to it.

If you’re going to share the access details for the live/production website, it is very important that a complete backup of your database and website has been made.

I would additionally need your permission to deactivate and reactivate Plugins and the Theme and to change configurations on the site. This is also a reason the backup is really important.

Regards,
Waqar

#1138189

Hi Ido,

Thank you for sharing the access details.

If you were referring to the empty filter for taxonomy "תחומי יצירה", I noticed that there are no terms added for that taxonomy yet ( screenshot: hidden link ).

Please add some terms for it and they'll show as options.

Next, I noticed that the custom filter field for "custom_query_type" was not available in the "Search and Pagination" section. Maybe it was removed since it is a live website?

To view the actual query, I would also suggest updating the PHP code I shared last time so that it brings in some hidden debugging information:


add_filter( 'wpv_filter_query', 'filter_cats_custom_fn', 1000 , 3 );
function filter_cats_custom_fn( $query_args, $view_settings ) {
 
	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 109) ) {

		echo '<div style="display: none;"><pre>';
		print_r($query_args);
		echo '</pre></div>';
 
		if (  (!empty( $query_args['tax_query'])) && (!empty( $_GET['custom_query_type'])) ) {
			$query_args['tax_query'][0]['operator'] = $_GET['custom_query_type'];
		}
	}
	return $query_args;
}

Lastly, I'd also request you to clarify with example(s), whether you're interested to change the query relationship type between individual taxonomy's multiple selected terms, or you'd like to change the query relationship type between multiple taxonomy filters? ( the solution, I shared was for the first case )

#1138228

Thanks Waqar! That did the trick 🙂

#1152145

hey there, sorry to reopen this, but it doesn't really work:

hidden link

it doesn't matter which opeiotn i choose, it always shows me "any" in the results...

any (no pun intended) idea?

thanks!
ido