Skip Navigation

[Resuelto] Creating 2 search filters, using the same "Filter by"

This support ticket is created hace 4 años, 11 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 10 respuestas, tiene 2 mensajes.

Última actualización por Waqar hace 4 años, 11 meses.

Asistido por: Waqar.

Autor
Mensajes
#1399541

Tell us what you are trying to do?
Creating 2 search filters for IEA CCC Authors (don't want to have 2 seperate fields because have to keep both fields sync manually), both are multiselect, one has an "AND" relationship, the other has a "OR" relationship.

Is there any way to
Keep both fields sync automatically, i.e. IEA CCC Author and IEA CCC Author 2,
or
Displaying at the front-end with IEA CCC Author, but the query is put into IEA CCC Author 2 parameter, and send through to the database asking for data of IEA CCC Author?

Is there any documentation that you are following?
No

Is there a similar example that we can see?
Go through the link, and I have created 2 filters, the one with "AND" is working, but the one with "OR" is not.

What is the link to your site?
enlace oculto
enlace oculto

#1399803

Hi,

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

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

Can you please share temporary admin login details?

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

regards,
Waqar

#1400775

Thank you for sharing the admin access but the page that you shared earlier is no longer showing the view with those author fields.
( enlace oculto )

It looks like that the view "Blogs Search" ( ID: 23662 ) has been modified and trashed.

Can you please share the link to a page with a view where both those identical author fields are available?

#1402773

Sorry, I might have trashed it accidentally.
It's now restored, the link is still
enlace oculto
thanks.

#1404259

Thank you for restoring the view.

Earlier when I checked this page, the fields for the "IEA CCC Authors" and "IEA CCC Authors 2" taxonomies, both had the exact same terms.

But now, only "IEA CCC Authors" taxonomy has the terms which are showing as options in the view, and all terms for "IEA CCC Authors 2" taxonomies have been removed.

With this change, it is not clear whether you'd like to keep these options in sync only in the front-end search form for the view or in the back-end?

If you could share more specific details around why you need two different fields (IEA CCC Author and IEA CCC Author 2) with the same options but with different operators ( i.e. "And" & "Or" ), I'll be in a better position to suggest a workaround.

#1405405

Hi Waqar

Earlier when you checked this page, I was using IEA CCC Authors options in IEA CCC Authors 2 field (I have changed some values in the shortcode) such that at the front-end it looks like it is "in sync".
But what I want is to keep them in sync at the back-end.
Thanks.

#1405467

Thank you for sharing that and I thought it might be the case.

If you absolutely require two separate custom taxonomies ("IEA CCC Authors" and "IEA CCC Authors 2") then you'll need to add the terms in them manually in the backend (even if they are same terms, as there is no feature available to keep them in sync).

Once you'll have the necessary terms in both taxonomies, the next step would be to assign them to relevant posts in the admin area.

Next, you'll include the filter for both these taxonomies in your view and using a custom JS script, the two filtering fields can be synced so that if some authors are selected in one of them, the same are selected in the other one as well. You can hide the one filtering field from visitors, using custom CSS code, so that visitors will only see one field for author selection but when the search form will be submitted the same author values will be submitted for both of them.

If this sounds like a workable solution, you can let me know once the view is ready with both fields and I'll be able to share some pointers around the custom CSS and JS code.

#1405845

What I am trying to do is let user pick which one of "Or" or "And" they want to search in.
Is there a way to do it?
Thanks.

#1406391

Thank you for clearing this up and it makes more sense now.

To confirm if I've understood this correctly, your goal is to have just one "IEA CCC Authors" taxonomy in the backend, but, when its filter is used on the front-end search form, you'd like visitor pick through another field if they'd like to perform "And" or "Or" search operation.

Is this correct?

Note: this can be achieved but will require some customization. I'll be able to share the steps after confirmation from your side and some testing on my own test website.

#1407059

Yes, that's what I mean.
That's good news to hear that it is achievable.

#1408151

Thank you for the confirmation.

During testing on my own website, I was able to make this search type filter work, but with a limitation, that it works only with the view that shows updated search results, through page reload and not AJAX.

This means that in the view's "Custom Search Settings" section, you'll need to select an option that reloads the page.
( screenshot: enlace oculto )

Here are the steps:

1. Since the "IEA CCC Authors 2" custom taxonomy is not needed, please remove it form custom taxonomies (Toolset -> Taxonomies) and also its filter from the view's "Query Filter" section.

2. Please also remove the extra author selector field's code from the "Search and Pagination" section, as we only need one field for authors now.
( screenshot: enlace oculto )

3. Adjust the query filter for the taxonomy, so that by default, it brings any of the matched values (OR).
( screenshot: enlace oculto )

4. In the "Search and Pagination" section, include the code of a custom search field, that will allow visitors to select the search operation type for authors.
( i.e. "Any of the selected" (OR) or "All of the selected" (AND) )


<div class="form-group">
	<label for="wpv-author-search-type">Author Search Type</label>
	<select id="wpv_control_select_wpcf-author-search-type" name="wpv-author-search-type" class="js-wpv-filter-trigger form-control">
		[wpv-conditional if="( '[wpv-search-term param="wpv-author-search-type"]' eq '' ) OR ( '[wpv-search-term param="wpv-author-search-type"]' eq 'IN' )"] 
		<option value="IN" selected="selected">Any of the selected</option>
		<option value="AND">All of the selected</option>
		[/wpv-conditional]
		[wpv-conditional if="( '[wpv-search-term param="wpv-author-search-type"]' eq 'AND' )"] 
		<option value="IN">Any of the selected</option>
		<option value="AND" selected="selected">All of the selected</option> 
		[/wpv-conditional]
	</select>
</div>

Note: This custom search field will automatically show the correct selected option after the page will reload as a result of the search form's submission, using the conditional output blocks ( ref: https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/ ) and the "wpv-search-term" shortcode ( ref: https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-search-term ).

5. To change the search operation type for this taxonomy field, based on the user's selection, you'll need to use a custom function hooked to "wpv_filter_query" filter ( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query ).


add_filter( 'wpv_filter_query', 'filter_emp_date_custom_fn', 1000 , 3 );
function filter_emp_date_custom_fn( $view_args, $view_settings, $view_id ) {

	if ( 	( !is_admin() && isset($view_settings['view_id'] ) ) && ( ($view_settings['view_id'] == 12345) ) )
	{
		if ( (empty($_GET['wpv-author-search-type'])) || (!isset($_GET['wpv-author-search-type'])) ) {
			$author_search_operator = 'IN';
		}
		else
		{
			$author_search_operator = $_GET['wpv-author-search-type'];
		}

		if ( !empty( $view_args['tax_query']) ) {
			$view_args['tax_query'][0]['operator'] = $author_search_operator;
		}		
	}

	return $view_args;
}

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 and replacing "12345" with the actual ID of the view.

I hope this helps and please let me know if any step is not clear.