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

Search shortcode

Relevanssi adds a shortcode to help making links to search results. That way users can easily find more information about a given subject from your blog. The syntax is simple: John Doe This will make the text John Doe a link to search results for John Doe. In case you…

Put sticky posts first in results

If you want to have sticky posts first in results when they match the search query, just add this code to your theme functions.php: add_filter( ‘relevanssi_hits_filter’, ‘rlv_sticky_first’ ); function rlv_sticky_first( $hits ) { $sticky = array(); $nonsticky = array(); $sticky_post_ids = get_option( ‘sticky_posts’ ); foreach( $hits[0] as $hit ) {…

Filter search results by date

You can specify date limits on searches with by_date search parameter. Here’s a link you can add to your search results page to offer your visitor the ability to restrict the search results to last day: <?php echo ‘<p><a href=”‘ . get_bloginfo(‘url’) . ‘?s=’ . get_search_query() . ‘&by_date=1d”>results from the…

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