Skip to main contentSkip to footer

This little filter function works on relevanssi_hits_filter. When a search query is made that matches an SKU (or any other custom field, but SKUs are the most likely scenario here), only results that match the SKU will be returned.

For this to work, Relevanssi must be set to index the _sku custom field (because otherwise, SKU search will not find anything), and the search query must match the SKU exactly.

Add this code to your site:

add_filter( 'relevanssi_hits_filter', 'rlv_sku_exact_match' );
function rlv_sku_exact_match( $hits ) {
    global $wpdb;
    $post_ids = $wpdb->get_col(
		$wpdb->prepare(
			"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sku' AND meta_value = %s",
			$hits[1]
		)
	);
    if ( ! $post_ids ) {
		// No matches found, don't touch the results.
        return $hits;
    }
	// Return only results with ID numbers that are in $post_ids.
	$hits[0] = array_filter(
		$hits[0],
		function( $hit ) use ( $post_ids ) {
            $hit_object = relevanssi_get_an_object( $hit )['object'];
			return in_array( $hit_object->ID, $post_ids, false );
		}
	);
	return $hits;
}

Any searches that match an SKU in the database should only return the product matching the SKU.

If you index product variation SKUs for the main product, the function above won’t work. When searching for product variation SKUs, it will insist that Relevanssi returns the variation product, but that’s not possible. Thus, Relevanssi returns no results. This version will return the main product:

add_filter( 'relevanssi_hits_filter', 'rlv_sku_exact_match' );
function rlv_sku_exact_match( $hits ) {
    global $wpdb;
    $post_ids = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sku' AND meta_value = %s",
            $hits[1]
        )
    );
    if ( ! $post_ids ) {
        // No matches found, don't touch the results.
        return $hits;
    }
    $post_ids = array_map(
        function( $post_id ) {
            if ( 'product_variation' === get_post_type( $post_id ) ) {
                return get_post_parent( $post_id )->ID;
            } else {
				return $post_id;
			}
        },
        $post_ids
    );
    // Return only results with ID numbers that are in $post_ids.
    $hits[0] = array_filter(
        $hits[0],
        function( $hit ) use ( $post_ids ) {
            $hit_object = relevanssi_get_an_object( $hit )['object'];
			return in_array( $hit_object->ID, $post_ids, false );
        }
    );
    return $hits;
}

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Category title in the search results page

If you’re using category restriction dropdown on your search form, here’s a bit of code that you can add to your search results template to show how many hits were found and what the category is. If no category was selected, this’ll just show how many hits were found. <?php……the category is. If no category was selected, this’ll just show how many hits were found. <?php $cat = get_cat_name( $wp_query->query_vars[‘cat’] ); $s = $wp_query->query_vars[‘s’]; echo $wp_query->found_posts . ” results found with ‘ $s'”; if ( ! empty( $cat ) ) { echo ” in the ‘$cat’ category“; } ?>…

Taxonomy archive search

This is a Premium feature and only applies when you index taxonomy terms. This does not apply in free Relevanssi. To include taxonomy pages (categories, tags, custom taxonomies) in the search, enable the option on the options page. There’s also the option to choose which taxonomies are included. By default…

Related Posts:

Comment Section:

1 Comment. Leave new

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed