Support » Plugin: Relevanssi - A Better Search » search order by sku, product name

  • Hi,
    I want to list by search first SKU then product name.
    In the example below, “SKU: TWIST” should be in the first place.

    Sample,
    SKU: TWIST
    Name: Metal USB Memory Stick

    SKU: PEN
    Name: TWIST Metal Ball Pen

    How can I do it?

    thanks for your help

    The page I need help with: [log in to see the link]

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

    (@msaari)

    See “Custom field -based weight” here: https://www.relevanssi.com/knowledge-base/relevanssi-match/

    Dear Mikko,
    I added the code, but it didn’t work.

    add_filter( 'relevanssi_match', 'custom_field_weights' );
    function custom_field_weights( $match ) {
        $featured = get_post_meta( $match->doc, '_sku', true );
        if ( '1' === $featured ) {
            $match->weight = $match->weight * 2;
        } else {
            $match->weight = $match->weight / 2;
        }
        return $match;
    }

    What could be the problem?

    • This reply was modified 1 month, 1 week ago by  doyuk.
    Plugin Author Mikko Saari

    (@msaari)

    That code won’t do anything useful: if a post has a SKU, it gets double weight and if it doesn’t have, its weight gets halved. All your products have a SKU, whether the search terms match the SKU or not doesn’t have any effect here.

    Try the snippet under “This function will increase the weight if the search term matches any custom field:”, which should give you a boost when the search matches the SKU.

    Dear Mikko,
    Thanks for the help.

    I’ve added the code to my site:

    add_filter( 'relevanssi_match', 'rlv_customfield_boost' );
    function rlv_customfield_boost( $match ) {
        if ( $match->customfield > 0 ) {
            $match->weight *= 10;
        }
        return $match;
    }

    At the moment I search for SKU “TWIST”, it lists products with the product name “TWIST”.
    I want SKU “TWIST” to be listed first.

    I’m sorry for my bad english.

    best regards

    Plugin Author Mikko Saari

    (@msaari)

    Try increasing the weight from 10, until the SKU match comes first – 10 isn’t a big boost, especially since title matches also get a boost. To get a better idea of how much to adjust, you can try using the Relevanssi admin search (Dashboard > Admin search) which will show you the scores for the posts.

    No need to apologize your English, it’s very good and perfectly understandable; you write better than many natives.

    • This reply was modified 1 month, 1 week ago by  Mikko Saari.
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.