Posted on

Indexing term descriptions for posts

Sometimes it is helpful to have Relevanssi index the taxonomy term descriptions for posts. For example, the term description can be a good place to add alternate search terms for the term. By default, Relevanssi only indexes the term name, so you need extra code to get the term descriptions.

The filter hook relevanssi_content_to_index is the tool for the job. It can be used to add extra content to posts as they are indexed. Add this function to your site (and replace post_tag with the name of the taxonomy you want to use):

add_filter( 'relevanssi_content_to_index', function( $content, $post ) {
	$terms = get_the_terms( $post->ID, 'post_tag' );
	if ( is_array( $terms ) ) {
		foreach ( $terms as $term ) {
			$content .= ' ' . $term->description;
		}
	}
	return $content;
}, 10, 2);

When you rebuild the index, the term descriptions are indexed for all the posts that have that term.

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 *