Posted on

Increasing weight for HTML tags

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.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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