Posted on

Indexing product codes with and without spaces

A Relevanssi Premium customer had a problem with product codes. The codes are in the format “ABC 100”, that is a group of letters and a group of digits with a space in between. Users may search for the codes without the space, so the post should be found with either “ABC 100” or “ABC100”.

The product codes don’t appear in a specific custom field, but instead in the post titles and post content. Thus the solution is to check both of those and whenever something is found that matches the product code pattern, that string is indexed as is, but also with the space removed.

The code is fairly straightforward:

add_filter( 'relevanssi_post_content', 'rlv_product_codes' );
add_filter( 'relevanssi_post_title_before_tokenize', 'rlv_product_codes' );
/**
 * Index product codes in the format "ABC 123" ie. group of letters,
 * a space, group of digits both ways, with or without the space, so
 * that the product can be found with either "ABC 123" or "ABC123" search
 * terms. This is applied to post content and titles.
 *
 * @param string $content The content to manipulate.
 *
 * @return string The content with the spaceless product codes added.
 */
function rlv_product_codes( $content ) {
	$pattern = '/(\w+) (\d+)/'; // Adjust pattern here if necessary.
	$hits    = preg_match_all( $pattern, $content, $matches );
	if ( $hits > 0 ) {
		$results  = array_map( function( $string ) { return str_replace( ' ', '', $string ); }, $matches[0] );
		$content .= ' ' . implode( ' ', $results );
	}
	return $content;
}

To implement this, add the code to the theme functions.php and then rebuild the index.

3 comments Indexing product codes with and without spaces

  1. Can this code be edited to work for product titles? For example when I search for “8320 P” I get the correct set of results, however, if I search for “8320P” I get zero results.

    Any suggestions/help is greatly appreciated.

    1. No need for editing for that, it already works for titles (and post content). However, the code is currently looking for product codes that are in the format “(some letters) (some numbers)”, when you need it to be the other way around.

      Change the pattern $pattern = '/(\w+) (\d+)/'; to $pattern = '/(\d+) (\w+)/';.

Leave a Reply to Merissa Kelley Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.