Custom Search Form, CPT, Multiple Text inputs
-
I have been working on creating a custom search form for a legal directory. I have a custom post type with 3 custom taxonomies. Some of these taxonomies have multiple words in them and that seems to be where my problem is.
I’m struggling to get the search to perform correctly when there is a taxonomy being searched for that has multiple words, for example, “accident reconstruction and safety” produces a “page not found” error, and from what I have gathered this is because WordPress does not allow for spaces in a search? Some taxonomies such as “law” (single word) work just fine.
I originally tried following this guide: https://www.smashingmagazine.com/2016/03/advanced-wordpress-search-with-wp_query/ from smashing magazine, however I could not get the pre_get_posts action hook to function properly, it ran every time a page was loaded and I could not get it to ever pick up on the parameters set in the input fields. So I abandoned that.
Then I was trying to use get_search_query() in the value attribute of the input fields on my custom search form (there are 3 of them) but I could find a way to replace spaces with dashes and ultimately this was failing as well.
Example of an input field with get_search_query()
<div class="inputGroup inputGroup_specality"> <label for="search__specality" class="label">Specalities</label> <input type="text" name="experts_specialities" value="<?php get_search_query() ?>" class="input" id="search__specality" placeholder="Ex: Specalities"> <div class="searchResults" id="searchResults_specality"></div> </div>
Next, I created a JS functions that listened for input and then replaced spaces with dashes and removed uppercase letters, and then set this new string as a value attribute, but when I tested this. While checking the console I verfied that it was updating the value attribute in the input fields however the search did not honor the attribute value but instead used the actual value in the input field.
Here is the custom search form code
<form role="search" action="<?php echo home_url( '/' ); ?>" method="get" class="expert_searchForm" autocomplete="off"> <input type="hidden" name="post_type" value="experts" /> <div class="searchForm__inputs"> <div class="inputGroup inputGroup_industry"> <label for="search__industry" class="label">Industries</label> <input type="text" onfocusout="getValue('search__industry')" name="experts_industries" value="" class="input" id="search__industry" placeholder="Ex: Medical failure, Accident Analysis"> <div class="searchResults" id="searchResults_industry"></div> </div> <div class="inputGroup inputGroup_specality"> <label for="search__specality" class="label">Specalities</label> <input type="text" name="experts_specialities" value="" class="input" id="search__specality" placeholder="Ex: Specalities"> <div class="searchResults" id="searchResults_specality"></div> </div> <div class="inputGroup inputGroup_location"> <label for="search_location" class="label">City</label> <input type="text" name="experts_locations" value="" class="input" id="search__location" placeholder="City, County or State"> <div class="searchResults" id="cityResults"></div> </div> <input type="submit" class="input input_button" id="search__button" value="Search"> </div> <div class="searchResults" id="searchResults"></div> </form>
Here is the JS function I came up with to create search friendly strings
function getValue (taxonomy) { let tax = document.getElementById(taxonomy).value; let newTax = tax.replace(/\s+/g, '-').toLowerCase(); document.getElementById(taxonomy).setAttribute('value', newTax); return 'nice'; }
Any help would be wildly appreciated.
Thanks.
- The topic ‘Custom Search Form, CPT, Multiple Text inputs’ is closed to new replies.