Posted on

Making post body not count

This function will re-calculate the post weights so that post body has no significance. Only titles, excerpts, categories and so on will effect the search results.

add_filter('relevanssi_match', 'no_post_body');
function no_post_body($match) {
    $title_boost = get_option('relevanssi_title_boost');
    $tag_boost = get_option('relevanssi_tag_boost');
    $link_boost = get_option('relevanssi_link_boost');
    $comment_boost = get_option('relevanssi_comment_boost');

    $idf = $match->weight / $match->tf;
    $match->tf = $match->title * $title_boost +
        $match->comment * $comment_boost +
        $match->tag * $tag_boost +
        $match->link * $link_boost +
        $match->author +
        $match->category +
        $match->excerpt +
        $match->taxonomy +
        $match->customfield;
    $match->weight = $match->tf * $idf;
    return $match;
}

Using this function will require Premium 1.5.4, as matches that get weight of zero are still included in the results in earlier versions.

Of course, a better solution would be to avoid indexing the words in post body in the first place, but then again this function can be added and removed on the go if necessary with no changes to the index necessary.

Update 31.5.2011: If you want to disable post content altogether, check out Disabling post content or title indexing.