WooCommerce Search by SKU or ISBN Finds Nothing? It Never Looks There

Date:

A shopper types the SKU from your catalog, or the ISBN printed on the back of a book, and gets “No products were found”. The product is right there, and the number is on its page. Search simply never reads that part of the product.

The short answer: WordPress search, which WooCommerce uses on the shop front end, matches only a product’s title, short description and description. The SKU and the attributes in the “Additional information” table (author, publisher, ISBN, brand) are stored as post meta, and the search query never touches post meta. So a WooCommerce search by SKU, ISBN or author finds a product only when the same words also happen to appear in its description. The fix is to give each product one searchable field built from those values, and make product search match it. A tested snippet that does both is below.

The value is on the product page, but search never reads the field it is stored in.

The symptom

On the test site, one product was set up the way many catalogs are: a title, a price, a SKU and an attributes table, with no description written yet.

FieldValue
TitleExample Field Guide to Garden Birds
SKUEX-1001
Author (attribute)A. N. Example
Publisher (attribute)Example Press
ISBN-13 (attribute)978-1-234567-89-7

Searching the shop from the front end gave this:

Typed into the search boxResult
garden birdsfound
EX-1001No products were found
A. N. ExampleNo products were found
Example PressNo products were found
978-1-234567-89-7No products were found
9781234567897No products were found

Every value is visible on the product page. Only the title words worked.

Check 1: where the value is stored

WooCommerce keeps a product’s title and descriptions in the posts table, the same place a blog post keeps its text. Everything else lives in post meta, one row per field: the SKU in _sku, the “GTIN, UPC, EAN, or ISBN” box on the Inventory tab (added in WooCommerce 9.2) in _global_unique_id, and the “Additional information” attributes in _product_attributes. A global attribute (one you set up under Products > Attributes) is stored as a taxonomy term instead, which search does not read either.

So the first question is simple: does the text you are searching for appear in the title, the short description or the description? If it only appears in the SKU box, the GTIN box or the attributes table, search cannot see it. On the test site, a second product with only its ISBN typed into the GTIN box gave “No products were found” for that ISBN as well.

Check 2: what WordPress search actually reads

You can see the exact query WordPress builds. For a product search for 9781234567897, the condition it generated on the test site was:

(wp_posts.post_title LIKE '%9781234567897%')
OR (wp_posts.post_excerpt LIKE '%9781234567897%')
OR (wp_posts.post_content LIKE '%9781234567897%')
Code language: SQL (Structured Query Language) (sql)

Three columns of the posts table, and no meta table at all. The WP_Query search parameters work the same way for every post type, products included. A multi-word query is split into words, and every word has to appear in one of those three columns.

That also explains a confusing result: on a product whose description says “by A. N. Example, published by Example Press”, the author search works. Search found the words in the prose, not in the attribute. Remove the description and the same search fails.

Check 3: the format trap

The ISBN was stored as 978-1-234567-89-7, with hyphens, which is how publishers print it. People type it as 9781234567897, or scan it from a barcode as digits. LIKE '%9781234567897%' does not match 978-1-234567-89-7, so even a search that does read the attribute misses it unless one side drops the hyphens. The same goes for SKUs with spaces or dashes. An ISBN is the same number whichever way it is written, so the search should treat it that way.

A testing trap: one result skips the results page

When you test a fix, watch for this. If a product search finds exactly one product, WooCommerce does not show a results page. It sends the visitor straight to that product. The check is in WooCommerce’s template functions, behind the woocommerce_redirect_single_search_result filter. A test that does not follow the redirect sees an almost empty response and reports “nothing found” for a search that worked.

The fix: make WooCommerce search by SKU, ISBN and attributes work

The clean way is to collect the values you want searchable into one field when the product is saved, then make product search also match that field. The snippet below does both:

  • On every product save it writes _pss_search_terms: the SKU, the GTIN/ISBN box, the attributes you list in pss_searchable_attributes(), and a digits-only copy of every ISBN-like value.
  • A posts_search filter finds the products whose field contains what the visitor typed, and adds them to the normal search with an OR. A query that looks like an ISBN has its hyphens and spaces removed first.

It changes nothing else: title and description search work as before, and admin screens are left alone.

/**
 * Search WooCommerce products by SKU, ISBN and chosen attributes.
 *
 * WordPress search only reads a product's title, description and short description. This snippet:
 *   1. builds one searchable text field per product when the product is saved: the SKU, plus the values of the
 *      attributes listed in pss_searchable_attributes(), plus a digits-only copy of every ISBN-like value;
 *   2. makes front-end product search also match that field. A query typed as an ISBN (978-1-23456-789-7,
 *      978 1234567897) is matched with its hyphens and spaces removed.
 *
 * Install as a Code Snippet ("Run everywhere") or in a small plugin. Then fill the field for the products you
 * already have (WP-CLI):
 *   wp eval 'foreach ( wc_get_products( array( "limit" => -1, "return" => "ids" ) ) as $id ) { pss_build_terms( $id ); }'
 *
 * License: MIT. Use, copy and change it freely; no warranty.
 */

if ( ! defined( 'ABSPATH' ) ) {
	return;
}

/** The attribute names to make searchable, as they appear in the "Additional information" table. */
function pss_searchable_attributes() {
	return array( 'ISBN', 'ISBN-13', 'ISBN-10', 'Author', 'Author(s)', 'Publisher', 'Brand', 'Model' );
}

/** Build the search field for one product. Runs on every product save. */
function pss_build_terms( $product_id ) {
	$product = wc_get_product( $product_id );
	if ( ! $product ) {
		return;
	}
	$values = array();
	if ( $product->get_sku() ) {
		$values[] = $product->get_sku();
	}
	if ( method_exists( $product, 'get_global_unique_id' ) && $product->get_global_unique_id() ) {
		$values[] = $product->get_global_unique_id();   // the "GTIN, UPC, EAN, or ISBN" field (WooCommerce 9.2+)
	}
	$wanted = array_map( 'strtolower', pss_searchable_attributes() );
	foreach ( $product->get_attributes() as $attribute ) {
		if ( ! is_object( $attribute ) ) {
			continue;
		}
		$label = wc_attribute_label( $attribute->get_name() );
		if ( ! in_array( strtolower( $label ), $wanted, true ) ) {
			continue;
		}
		$options = $attribute->is_taxonomy()
			? wc_get_product_terms( $product_id, $attribute->get_name(), array( 'fields' => 'names' ) )
			: $attribute->get_options();
		foreach ( $options as $option ) {
			$values[] = $option;
		}
	}
	$terms = array();
	foreach ( $values as $value ) {
		$value = trim( wp_strip_all_tags( (string) $value ) );
		if ( '' === $value ) {
			continue;
		}
		$terms[] = $value;
		$digits  = str_replace( array( '-', ' ' ), '', $value );
		if ( $digits !== $value && preg_match( '/^(\d{13}|\d{9}[\dXx])$/', $digits ) ) {
			$terms[] = $digits;   // an ISBN as people type it
		}
	}
	update_post_meta( $product_id, '_pss_search_terms', implode( ' | ', array_unique( $terms ) ) );
}
add_action( 'woocommerce_new_product', 'pss_build_terms' );
add_action( 'woocommerce_update_product', 'pss_build_terms' );

/** Make front-end product search also match the search field. */
function pss_posts_search( $search, $query ) {
	if ( '' === $search || ! $query->is_search() || ( is_admin() && ! wp_doing_ajax() ) ) {
		return $search;
	}
	if ( ! in_array( 'product', (array) $query->get( 'post_type' ), true ) ) {
		return $search;
	}
	$typed  = trim( (string) $query->get( 's' ) );
	$digits = str_replace( array( '-', ' ' ), '', $typed );
	$needle = preg_match( '/^(\d{13}|\d{9}[\dXx])$/', $digits ) ? $digits : $typed;
	if ( '' === $needle || 0 !== strpos( $search, ' AND ' ) ) {
		return $search;
	}
	$ids = get_posts( array(
		'post_type'        => 'product',
		'post_status'      => 'publish',
		'has_password'     => false,
		'fields'           => 'ids',
		'posts_per_page'   => 200,
		'no_found_rows'    => true,
		'suppress_filters' => true,
		'meta_query'       => array(
			array( 'key' => '_pss_search_terms', 'value' => $needle, 'compare' => 'LIKE' ),
		),
	) );
	if ( empty( $ids ) ) {
		return $search;
	}
	global $wpdb;
	$in = implode( ',', array_map( 'intval', $ids ) );
	return ' AND ( ' . substr( $search, 5 ) . ' OR ' . $wpdb->posts . '.ID IN ( ' . $in . ' ) )';
}
add_filter( 'posts_search', 'pss_posts_search', 20, 2 );
Code language: PHP (php)

Products you already have get their field the next time they are saved. To fill it for all of them at once, run the one-line WP-CLI command from the comment at the top. Edit pss_searchable_attributes() to match the attribute names your store actually uses.

With the snippet active, the same test product gave:

Typed into the search boxBeforeAfter
garden birdsfoundfound
EX-1001No products were foundfound
A. N. ExampleNo products were foundfound
Example PressNo products were foundfound
978-1-234567-89-7No products were foundfound
9781234567897No products were foundfound

Build the searchable text once, when the product is saved, and keep the search itself simple.

Where product search looks, and where your SKU livesThe same product, searched by title and by SKU, before and after one search field is added
What search reads

Title, short description and description. Nothing else.

Where the SKU lives

SKU and attributes (author, ISBN) are post meta: never searched.

no products found
The fix

Build one search field on save; match it with posts_search. Now it is found.

Tested on WordPress 7.0.9 with WooCommerce 10.8.1: title search found the test product; SKU, author, publisher and ISBN searches found it only with the search field.

Other ways to fix it

  • A search plugin. A plugin such as Relevanssi builds its own index and can include custom fields. It is the better choice when you also want relevance ranking, typo tolerance or search across many content types. It is one more plugin to keep updated.
  • Repeat the values in the description. Writing “ISBN 9781234567897, by A. N. Example” into each description makes the plain search find them. It works with no code, but it mixes data into copy and breaks the moment the description is rewritten.
  • Global attributes for filters. If shoppers also need to filter by author or brand, set those up as global attributes under Products > Attributes. That gives you filter widgets. It does not, on its own, make them searchable.

Edge cases worth checking

  • One product page for several editions. If one page sells a paperback and a hardcover, each with its own ISBN, put every edition’s ISBN into the search field, not only the one shown in the table. Otherwise a search for the second ISBN finds nothing.
  • Old pages that redirect to a new one. If you merge products and leave the old page redirecting, take the old page out of search results too, or the same item shows up twice.
  • Speed. A LIKE '%…%' search on post meta cannot use an ordinary index, so it reads every row with that meta key. For a few thousand products that is fast. For a very large catalog, use a search plugin with its own index, and see what database indexes can and cannot speed up.
  • The Store API and blocks. The snippet works on the classic front-end search, the one the theme’s search box submits. Searches made through the WooCommerce Store API, which some product blocks use, behaved differently on the test site and need their own check.
  • Other product snippets. Hooking product behavior has the same priority traps as any WooCommerce filter. The block editor for WooCommerce products article walks through one.

The short version

WooCommerce product search reads only the title, short description and description. SKUs and “Additional information” attributes live in post meta, which search never reads, and ISBNs stored with hyphens never match the digits people type. Build one searchable field per product on save, from the SKU, the attributes that matter and digits-only ISBNs, and add it to search with a posts_search filter. Test with a search tool that follows redirects, because a single match skips the results page.

Frequently Asked Questions

Why does the admin product list find SKUs when the shop does not?

The admin product list uses WooCommerce’s own product search, which also matches the SKU and the GTIN/ISBN box. The shop front end uses the normal WordPress search query, which matches neither. Both search boxes look the same, so the difference is easy to miss: a store owner tests in wp-admin, finds the product, and never sees what shoppers see.

Does this find the SKUs of a variable product’s variations?

Not as written. Each variation is its own post with its own SKU, and the snippet reads the parent product only. To cover variations, add each variation’s SKU to the parent’s search field when the parent is saved, by looping over $product->get_children().

Share post:

Subscribe

Popular

More like this
Related

Computer Slow but CPU and Memory Look Normal? Check the Handle Count

Chrome takes a minute to open, or doesn't open at all. Every click waits. You open Task Manager expecting to find the culprit, and it tells you everything is fine. Here is the number that isn't, and the fix that takes two minutes.

Kernel-Power 41 Tells You Your PC Restarted, Not Why

Your PC restarts with no blue screen, and the only clue is a critical event saying it didn't shut down cleanly. Here is how to get from that event to a short list of suspects.

Set Up WordPress SMTP Without a Plugin, Even on a Multisite Network

WordPress mail goes out as wordpress@ your domain, from the web server, and lands in spam. An SMTP plugin fixes that. So do about twenty lines of code, and on a network of sites the code can give every site its own mailbox and its own password.

Stop WordPress Content Scraping Through Feeds, the REST API and Load More

Someone copies your whole archive and never crawls a single page. A default WordPress site hands every post's full text to any visitor who asks the right three addresses.