Skip to main contentSkip to footer

WooCommerce: Return only exact matches for SKU searches

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

Indexing embedded PDFs for the parent post

…WordPress. Thus, Relevanssi won’t know the PDF is embedded in the post and cannot index the PDF contents for the parent post. Most of these plugins use shortcodes to embed the PDF viewer on a page. To get Relevanssi to index the embedded PDF contents for the parent post, you……$upload_dir = wp_upload_dir(); foreach ( $matches[1] as $pdf ) { $pdf_url = ltrim( str_replace( $upload_dir[‘baseurl’], ”, urldecode( $pdf ) ), ‘/’ ); $pdf_content = $wpdb->get_var( $wpdb->prepare( “SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = ‘_relevanssi_pdf_content’ AND post_id IN ( SELECT post_id FROM $wpdb->postmeta WHERE meta_key = ‘_wp_attached_file’ AND meta_value = %s……meta_key = ‘_wp_attached_file’ AND meta_value = %s )”, $pdf_url ) ); $content .= $pdf_content; } } return $content; } If you use the Gutenberg block, the code is different and uses the relevanssi_block_to_render filter hook: add_filter( ‘relevanssi_block_to_render’, ‘rlv_pdfembedder_content’, 10 ); function rlv_pdfembedder_content( $block ) { if ( $block[‘blockName’] === ‘pdfemb/pdf-embedder-viewer’…

How to include specific posts
Hello, I’m using WooCommerce and Relevanssi and they work great.My Site Search only searches Products, however I want to let…
EmbedPress autoembeds
EmbedPress has an auto-embed feature that tries to embed all URLs found in posts. This can cause problems with Relevanssi:…

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