Skip to main contentSkip to footer

Relevanssi ignores HTML tags when indexing posts. If you want to give more weight to words that appear in specific HTML tags (like headings), here is one way to do that. You can add this filter function on your site to the theme functions.php or in a code snippet:

add_filter( 'relevanssi_post_content', 'rlv_html_tag_boost' );
function rlv_html_tag_boost( $content ) {
	$weightings = array(
		'h1'     => 5,
		'h2'     => 4,
		'h3'     => 2,
		'strong' => 1,
	);
	foreach ( $weightings as $tag => $weight ) {
		if ( preg_match_all( "#<$tag.*?>(.*?)</$tag>#", $content, $elements ) ) {
			foreach ( $elements[1] as $el_content ) {
				while ( 0 < $weight-- ) {
					$content .= " $el_content";
				}
			}
		}
	}
	return $content;
}

This code goes through the post content during the indexing with the relevanssi_post_content filter hook. When it finds content that appears inside the HTML tags specified in the $weightings array, it will add the content inside those tags multiple times in the post content (as determined by the numbers in the $weightings array). This multiplication will increase the weight of those words in the Relevanssi index because Relevanssi counts how many times each word appears in each document.

Thanks to Greg Perham from Swing Graphics for this clever solution for easily adjusting the weights.

Your account

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

Search

Popular Resources

Martfury
Martfury is a WooCommerce theme that comes with a built-in dropdown search. That’s great, except that as usual the dropdown…
WP Event Manager
Using Relevanssi with WP Event Manager requires you to adjust the search process in WP Event Manager a bit. Fortunately,…

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