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

10Web Photo Gallery
The Photo Gallery plugin from 10Web uses a shortcode to add photo galleries to posts and pages. By default, Relevanssi…
SearchWP Live Ajax Search
Relevanssi doesn’t ship with a live search or autocomplete feature that would display results as the user types the search…

Related Posts:

Comment Section:

1 Comment. Leave new

Leave a Reply to Jim Cancel 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