Select Multiple Terms Through a Drop Down? (Can't Use Checkbox)
-
I am wondering if there is a way to modify the dropdown selector to be able to select multiple search terms. I know that I could use checkboxes for multiple terms but I have a huge list of terms and I don’t want all of them to display unless the user clicks on the dropdown item. More than likely the user will need to select more than 1 term so I am hoping that by holding (shift) or (ctrl) that they can select/highlight more than 1 term at a time.
I have tried to add
multiple="MULTIPLE"
within the code of theuwpqsf-front-class.php
file but it doesn’t seem to effect the front end form.Any help is much appreciated!!
https://www.ads-software.com/plugins/ultimate-wp-query-search-filter/
-
Don’t edit the plugin core files, or you will have problem when you updates the plugin.
In order to use Multiple, you will have to use filters that provided by the plugin. This will have to do following step.
#1. Add an option in the backend add_filter('uwpqsftaxo_field', 'add_multiselect_admin'); function add_multiselect_admin($fields){ $fields['multiselect'] = 'Multi Select'; return $fields; } #2. Add the field to the frontend add_filter('uwpqsf_addtax_field_multiselect','multiselect_front','',11); function multiselect_front($type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){ $eid = explode(",", $exc); $args = array('hide_empty'=>$hide,'exclude'=>$eid ); $taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid); $terms = get_terms($taxname,$taxoargs); $count = count($terms); $html = '<div class="'.$defaultclass.' '.$divclass.'" id="tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>'; $html .= '<input type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">'; $html .= '<input type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">'; $html .= '<select multiple id="tdp-'.$c.'" class="tdp-class-'.$c.'" name="taxo['.$c.'][term]">'; if(!empty($taxall)){ $html .= '<option selected value="uwpqsftaxoall">'.$taxall.'</option>'; } if ( $count > 0 ){ foreach ( $terms as $term ) { $selected = (isset($_GET['taxo'][$c]['term']) && $_GET['taxo'][$c]['term'] == $term->slug) ? 'selected="selected"' : ''; $html .= '<option value="'.$term->slug.'" '.$selected.'>'.$term->name.'</option>';} } $html .= '</select>'; $html .= '</div>'; return $html; }
That’s it. You will see a Multi Select option in the display type on the form setting page, taxonomy field.
The code works great TC.K! Thank you so much for the unbeleivable plugin and for your help!
I just have one question, what is the best way to edit the height/size of the new multi-select box? Can I accomplish this within the code in my functions.php file, or do I need to target it through CSS?
Thanks again!
Never mind, I just targeted the multi-select field in my style.css and changed the height.
Thank you again for your help TC.K, you are awesome!
Hey TC.K I just realized that while I can select multiple terms on the frontend it doesn’t actually search for all of the terms selected, it only uses the last term selected. For example, if the user selects Red, Blue and Green in the multiselect box the only term that will be queried is Green.
Any ideas?
Try this, on this line:
$html .= '<select multiple id="tdp-'.$c.'" class="tdp-class-'.$c.'" name="taxo['.$c.'][term]">';
append
[]
to thename
value:
$html .= '<select multiple id="tdp-'.$c.'" class="tdp-class-'.$c.'" name="taxo['.$c.'][term][]">';
I think that worked, but now I can’t get the multi-select values to display on the frontend search results where is says “Search results for — ” ”
I am using the following function in my
functions.php
file to display the terms used in the query:add_filter( 'get_search_query', 'uwpqsf_var', 20, 1 ); function uwpqsf_var($s){ if(is_search() && isset($_GET['s']) && $_GET['s'] == 'uwpsfsearchtrg' && isset($_GET['uformid']) ){ if(isset($_GET['taxo'])){ foreach($_GET['taxo'] as $v){ if(isset($v['term'])){ if($v['term'] == 'uwpqsftaxoall'){ }else{ $termname = get_term_by('slug',$v['term'],$v['name']); $var[] = $termname->name; } } } if(!empty($_GET['skeyword'])){ $var[] = $_GET['skeyword']; } $return = ''; if(!empty($var)){ $return = implode(' | ', $var); } return $return; } }else{ return $s; } }
How can I edit the above function to include the terms selected from the multi-select?
Thanks!
You can refer to this thread for displaying the array term
Hi, TC.K your plugin is excellent. I was wondering if it’s possible to have a similar solution as SJS719 requested before but I need to have a dropdown with multiple checkboxes. Thank you!
@pizanim, by default html doesn’t have dropdown with multiple checkboxes input type. In order to do so, you will have to using js to do it.
It’s more depends on the js you are using, I can’t tell the solution.Hello TC.K
I’ve set the correction you suggested above for multiple select, but leaving a multiple select on the default option always returns no results. Seems to be because of uwpqsftaxoall becoming an array.Here are prints on the “taxo” get variables with normal and multi select on just the first:
Array ( [0] => Array ( [name] => zona [opt] => 1 [term] => Array ( [0] => uwpqsftaxoall ) ) [1] => Array ( [name] => invecinare [opt] => 1 [term] => uwpqsftaxoall ) [2] => Array ( [name] => tip-de-proprietate [opt] => 1 [term] => uwpqsftaxoall ) [3] => Array ( [name] => facilitate [opt] => 1 [term] => animale ) )
this one has the first element as a multi select and returns no results
Array ( [0] => Array ( [name] => zona [opt] => 1 [term] => uwpqsftaxoall ) [1] => Array ( [name] => invecinare [opt] => 1 [term] => uwpqsftaxoall ) [2] => Array ( [name] => tip-de-proprietate [opt] => 1 [term] => uwpqsftaxoall ) [3] => Array ( [name] => facilitate [opt] => 1 [term] => animale ) )
this one is with all selects and its fine.
the same stuff is selected in both cases.
Thanks!
Try no insert ‘Text for search all option’.
I am also interested in this topic but, as SJS719 pointed out, the code isn’t complete because terms are not remembered on the front-end. So, I went ahead and modified it to work correctly. I also want to use hierarchical taxonomy listings which your code does not do so I added that feature as well. I am sure my code isn’t the best possible but it seems to work. It would be great if you incorporated it along with an admin screen option to choose hierarchical display or not in a future release. Anyway, here is my final code.
add_filter('uwpqsf_addtax_field_multiselect','multiselect_front','',11); function multiselect_front($type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){ $eid = explode(",", $exc); $args = array('hide_empty'=>$hide,'exclude'=>$eid, 'parent'=>0 ); $taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid); $terms = get_terms($taxname,$taxoargs); $count = count($terms); $html = '<div class="'.$defaultclass.' '.$divclass.'" id="tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>'; $html .= '<input type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">'; $html .= '<input type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">'; $html .= '<select size="4" multiple id="tdp-'.$c.'" class="tdp-class-'.$c.'" name="taxo['.$c.'][term][]">'; if ( empty($_GET['taxo'][$c]['term']) || in_array("uwpqsftaxoall", $_GET['taxo'][$c]['term']) ) { $alloptions .= '<option selected value="uwpqsftaxoall">'.$taxall.'</option>'; } else { $alloptions .= '<option value="uwpqsftaxoall">'.$taxall.'</option>'; } if ( $count > 0 ) { $i = 0; foreach ( $terms as $term ) { if ( isset($_GET['taxo'][$c]['term']) && in_array($term->slug,$_GET['taxo'][$c]['term']) ) { $i++; if ($i<=1) { foreach($_GET['taxo'][$c]['term'] as $slug) { $termname = get_term_by('slug',$slug,$_GET['taxo'][$c]['name']); $preselected .= '<option value="'.$termname->slug.'" selected="selected">'.$termname->name.'</option>'; } } } else { $options .= '<option value="'.$term->slug.'">'.$term->name.'</option>'; $childterms = get_terms($taxname,array('parent'=>$term->term_id )); if ( !empty($childterms) ) { foreach ($childterms as $childterm) { if ( !in_array($childterm->slug,$_GET['taxo'][$c]['term']) ) $options .= '<option value="'.$childterm->slug.'"> - '.$childterm->name.'</option>'; } } } } } $html .= $alloptions . $preselected . $options; $html .= '</select>'; $html .= '</div>'; return $html; }
Here’s an update on the code I just posted. It does work fine (so far) except for one major problem. It will NOT work if you select the All option. This is because your
get_uwqsf_taxo
function doesn’t check this condition in an array situation. In other words, your line of code:
if( $v['term'] == 'uwpqsftaxoall'){
needs to be changed to something like:
if( $v['term'] == 'uwpqsftaxoall' || $v['term'][0] == 'uwpqsftaxoall') {
Doing that fixed everything for me, but since it is part of the core plugin file it is obviously not an upgrade-proof solution.
@mojamba, you can use
uwpqsf_get_cmf
to customize the query.Thanks. I have been looking into your suggestion but cannot seem to figure it out. Can you provide more details or possibly a code example?
- The topic ‘Select Multiple Terms Through a Drop Down? (Can't Use Checkbox)’ is closed to new replies.