As of version 3.1, SearchWP supports “did you mean?”-style search corrections when a search yields no results and SearchWP can determine what the visitor likely meant to search for.
To enable this functionality, tick the appropriate checkbox on the Advanced tab of the SearchWP settings screen:
(Alternatively you can also enable this behavior using this hook: searchwp_do_suggestions
)
When this feature is enabled, SearchWP will automatically attempt to correct any searches that provide zero results in addition to outputting a notice above The Loop of your search results:
⚠️ Style (CSS) customization is likely required ⚠️
If you would like to prevent the automatic output of this notice you can use the searchwp_auto_output_revised_search_query
hook to disable it. Subsequently you can use the searchwp_revised_search_query
to implement your own in place of the automatic output.
The above screenshot outlines the automatic output in Twenty Nineteen, a default WordPress theme. The following CSS can be added to improve its appearance:
Note: (You can add Custom CSS to your site by navigating to Appearance > Customize > Additional CSS in your WordPress Dashboard)
.searchwp-revised-search-notice { | |
margin: 0 calc(10% + 60px) 1em; | |
padding: 0.6em; | |
background: #f7f7f7; | |
border: 1px solid #e7e7e7; | |
border-radius: 2px; | |
} | |
.searchwp-suggested-revision-query { | |
font-style: italic; | |
font-weight: bold; | |
} |
Which results in a notification that better matches the site design:
You can override the default output by adding something like this to your theme’s functions.php
:
<?php | |
// Override SearchWP's "Did you mean?" output. | |
class MySearchwpDidYouMean { | |
private $args; | |
function __construct() { | |
// Prevent SearchWP's automatic "Did you mean?" output. | |
add_filter( 'searchwp_auto_output_revised_search_query', '__return_false' ); | |
// Grab the "Did you mean?" arguments to use later. | |
add_action( 'searchwp_revised_search_query', function( $args ) { | |
$this->args = $args; | |
} ); | |
// Output custom "Did you mean?" message at the top of The Loop. | |
add_action( 'loop_start', function() { | |
if ( empty( $this->args ) ) { | |
return ''; | |
} | |
$phrase_query = str_replace( array( '”', '“' ), '"', SWP()->original_query ); // Accommodate curly quotes. | |
echo '<p class="searchwp-revised-search-notice">'; | |
echo wp_kses( | |
sprintf( | |
// Translators: First placeholder is the quoted search string. Second placeholder is the search string without quotes. | |
__( 'No results found for <em class="searchwp-revised-search-original">%s</em>. Showing results for <em class="searchwp-suggested-revision-query">%s</em>', 'searchwp' ), | |
esc_html( $phrase_query ), | |
esc_html( str_replace( '"', '', implode( ' ', $this->args['terms'] ) ) ) | |
), | |
array( | |
'em' => array( | |
'class' => array(), | |
), | |
) | |
); | |
echo '</p>'; | |
} ); | |
} | |
} | |
new MySearchwpDidYouMean(); |
Interoperability with partial matches
When “Did you mean?” functionality is enabled and partial matching is enabled, SearchWP will first attempt to find exact partial matches and utilize partial match functionality.
If no partial matches are found, SearchWP will then utilize its “Did you mean?” functionality, determining the best search correction it can. If no search correction can be determined, SearchWP will show zero results.