If you want to add an option to search only in titles to your search form, here’s how. First, you need to modify the search form to add the selection. To add a simple dropdown, add this to your search form:
<select name="titles"> <option value="0">Search Everything</option> <option value="1">Search only in Titles</option> </select>
This select
element will create a small dropdown with the option to search everything or only in titles. It will set the titles
query parameter to 0 or 1, depending on the user selection. We can then look for that parameter and when the parameter value is 1, ignore everything that isn’t a title match.
add_filter( 'relevanssi_match', function( $match ) { if ( 1 === intval( $_GET['titles'] ) ) { if ( $match->title < 1 ) { $match->weight = 0; } } return $match; } );
This function will set the match weight to 0 if the word doesn’t match a title.