Search code examples
wordpresssearchclosestwpaspartial-matches

Wordpress search with similar/partial results based on taxonomy


I'm working on a directory which I've created in Wordpress. The directory lives as a custom post type called "agents" and has it's own taxonomy called "agent_location". Each agent has multiple "agent_location" terms added to their listing. Each agent_location term is only connected to one "agent". Each agent_location is the beginning of a UK postcode. For example "AB1 1" or "AB11 3" etc.

I'm using WPAS Wordpress Advanced Search (http://wpadvancedsearch.com/) to create the search forms. I would like the end user to be able to type their entire postcode for example AB1 1AA and be presented with the agent post that has the term "AB1 1" and not the agent post that has "AB11 3" listed. Essentially the search needs to ignore the last 2 digits of any search entered. Currently the form I've created looks like this:

function agents_search_form() {
$args = array();
$args['wp_query'] = array('post_type' => 'agents',
                          'posts_per_page' => -1);

$args['fields'][] = array('type' => 'search',
                          'title' => 'Search',
                          'placeholder' => 'Enter search terms...',
                            'pre_html' => '<div class="hiddenField searchRow"><div class="searchWrap">',
                            'post_html' => '</div>');

$args['fields'][] = array( 'type' => 'submit',
                           'class' => 'button',
                           'value' => 'Search' ,
                            'pre_html' => '<div class="searchBTNWrap">',
                            'post_html' => '</div></div>');

$args['fields'][] = array('type' => 'taxonomy',
                          'taxonomy' => 'agent-location',
                          'format' => 'text');


register_wpas_form('agents-form', $args); 
}
 add_action('init', 'agents_search_form');  

When you use the form it works perfectly providing you don't include the last 2 digits of your postcode. If you type your entire postcode no search results will be found. I would like help creating a search form that allows users to type their entire postcode into the search and still get a result.

I'm open to the idea of workarounds. Please bare in mind that:

  • The postcodes that each agent post lists are spread all over the UK. There is no central location to work from so "radius" map searches won't work.
  • I'm happy to move the taxonomy into the post directly as a custom field if it helps
  • The website is already live so I don't want to recreate the entire thing but I'm happy to code new solutions or use plugins if it get the results I want. The website currently uses Gravity forms, WPAS, Beaver Builder and ACF pro.
  • A large portion of the website visitors are blind or partially sighted so solutions must be "screen reader friendly"

Thanks in advance


Solution

  • I ended up using the Relevanssi plugin - https://wordpress.org/plugins/relevanssi/ and combining it with WPadvancedsearch, it actually works well together and let me do everything I needed.