Support » Plugin: Relevanssi - A Better Search » Adding weight headings in post content

  • Allon Sacks

    (@allonsacksgmailcom)


    Hi,

    Is it possible to add weights to post based on the search word being in and h2 tag etc?
    We would like to add considerable weight is the search term is an h2 and if there is an exact match display it in the search results instead of the search title… so kinda 2 questions here.

    Is it possible at all? In regular or premium version?
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Relevanssi is flexible enough that almost everything is possible – but this is going to take some custom programming to implement.

    You can use relevanssi_match hook to adjust the post weights. There’s some documentation here.

    You’d need to write a function that would check to see if the search term appears in a h2 tag and if it does, then you’d boost the weight.

    In practise, that does sound like it would likely make the search quite a bit slower. If this is important feature for you, I’d consider code that reads the post when it’s saved and puts the h2 tag content in a custom field and then boosts the weight if the search query matches that custom field – that might be more efficient (I assume it’s faster to check a custom field than to do bunch of string matching operations for every post).

    So, possible, but complicated. Relevanssi doesn’t care about the post structure at all. (This is something I might look into in the future: perhaps extracting the various levels of headings from the post might make sense. But that’d certainly be a Premium feature.)

    As for the second question, that’s outside the scope of Relevanssi and something your search results template needs to do. Doesn’t sound too complicated to me.

    Allon Sacks

    (@allonsacksgmailcom)

    Thanks!

    Regarding the second question I got it done already I forgot it has been time since I set it up 🙂

    Regarding the first I appreciate your input.

    I got it done, seems not to slow things down too much.
    Pasting the code if anyone wants it below but first another question.

    It seems if the text in the h2 is in quotes or with hyphens it gets completely overlooked. So in my function below, match does not even return posts where the query has quotes or hyphens inside it. Any idea why this is happening or what to do about it?

    Thanks! and here is my code:

    function getTextBetweenTags($string, $tagname) {
    	$pattern = "/<$tagname>(.*?)<\/$tagname>/";
    	preg_match_all($pattern, $string, $matches);
    	return $matches[1];
    }
    add_filter( 'relevanssi_match', 'h2_match' );
    function h2_match($match) {
    	$searchQuery = strtolower(get_search_query());
    	$content_post = get_post($match->doc);
    	$content = strtolower($content_post->post_content);
    	$content = apply_filters('the_content', $content);
    	$content = str_replace(']]>', ']]>', $content);
    	$content2 = getTextBetweenTags($content, 'h2');
    	foreach($content2 as $key=> $value) {
    		if (strpos($value,$searchQuery) !== false) {
    			$match->weight = $match->weight * 30;
    		}
    	}
    	return $match;
    }
    Plugin Author Mikko Saari

    (@msaari)

    The hyphens and quotes might be URL encoded in $searchQuery – I’d check that. Make the function print out the values and see what happens, that would be a fairly easy way to debug this.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding weight headings in post content’ is closed to new replies.