Skip to main contentSkip to footer

Search form: Search only in titles

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

Pin a post for all searches
Relevanssi Premium has tools for pinning posts for specific queries and all searches where the post appears, but what if…
ThemeCo

ThemeCo themes use custom codes for dynamic content. Those are not usual shortcodes, and Relevanssi won’t expand them automatically. In order to expand the dynamic content from ThemeCo themes, you need to use these filters: add_filter( ‘relevanssi_post_content’, ‘relevanssi_filter_the_content’, 8, 2 ); add_filter( ‘relevanssi_pre_excerpt__content’, ‘relevanssi_filter_the_content’, 8, 2 ); function relevanssi_filter_the_content( $post_content,…

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