Skip to main contentSkip to footer

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.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Index only short numbers
Relevanssi has a minimum word length setting that is set to three characters. Any one- or two-letter words are not…
Indexing the post ID
Are post ID’s searchable in Relevanssi? No. Relevanssi does understand the WP_Query p parameter to restrict the search by post…

Related Posts:

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed