Search a WooCommerce product table by description, excerpt and SKU
The search field above a products table looks in product titles only, so a shopper who types a word from the description finds nothing. To search a WooCommerce product table by description, short description or SKU, you widen what that field searches with one shortcode attribute, or add separate search fields to the table's filter. Below is how the built-in search works, how to extend it, and why a search field sometimes seems to do nothing.
How the built-in search of a products table works
Every table has a search by title field above it. It is controlled by three rows on the table's Options tab:
- Show search by title: shows or hides the field.
- Min symbols count: the shortest search the field accepts, 3 by default.
- Search by title placeholder: the hint text inside the field.
The same can be set per shortcode with hide_text_search=1, text_search_min_symbols=2 and text_search_placeholder='Find a product'. Where the field sits relative to the filter form is explained in how to set the position of the filter by post title.
Three details of its behaviour explain most "the search does not work" reports:
- It searches on Enter, not while typing. The table reloads its rows when the shopper presses Enter (the search key on a phone keyboard). Esc or the small × in the field clears the search.
- Too short a search is ignored. With fewer characters than Min symbols count, the field gets a red dashed border and nothing is sent. Lower the value if your products have short names or codes.
- It matches the text as typed. The search finds products whose title contains the typed text as one piece, ignoring letter case. red shirt finds "Red shirt, long sleeve" but not "Shirt in red".
Search the WooCommerce product table by description and excerpt
In WooCommerce the long product description is stored as the post content (post_content) and the short description as the excerpt (post_excerpt). The woot_text_search_by attribute of [woot] sets which of these fields the search field looks in:
[woot id=1 woot_text_search_by='post_title,post_content,post_excerpt']
A product is found when any of the listed fields contains the search text. Any combination works, for example 'post_title,post_excerpt' to leave long descriptions out. Three rules for the value:
- Comma separated, no spaces. Each name must match exactly. With
'post_title, post_content'the second name starts with a space, is not recognised and is silently dropped, so the search stays on titles. - Straight quotes. Editors that turn
'into’glue the quote to the first and last names, and both are dropped. If no valid name is left, the search filters nothing and every search returns all products. Type the shortcode in a Shortcode block or code editor, or leave the quotes out:woot_text_search_by=post_title,post_content,post_excerptworks too. - Only these three fields.
post_title,post_contentandpost_excerptare the accepted names; anything else is ignored. SKU and custom fields are searched through the filter, as shown below.
The setting also applies to the Title element of the table's filter form, because both fields send the same search. The text found in the description is not highlighted; to let shoppers see it, add the Content or Excerpt column.
To set the fields for a table everywhere it appears, including the [woot_drop_down] search described below, which has no such attribute, add them by code:
/*
* Products table #1: the search field also looks in the description
* and the short description. Child theme functions.php or a code snippets plugin.
*/
add_filter('woot_review_shortcode_args', function ($args) {
if (isset($args['id']) && (int) $args['id'] === 1) {
$args['woot_text_search_by'] = 'post_title,post_content,post_excerpt';
}
return $args;
});
Separate search fields and SKU search in the filter
Instead of one field for everything, the table's Filter tab can show separate text fields: Title, Content, Excerpt and SKU. Each searches only its own field, they combine with each other and with the other filter elements, and like the main field they run on Enter. In their options each has a Placeholder and a Width, and the title, content and excerpt fields also a Minlength.
The SKU element is the way to search by SKU. Its Search logic option chooses between LIKE (the SKU contains the typed text, so A12 also finds A123) and EXACT (the whole SKU must match). Two limits:
- It searches the SKU of the product in the row. In a regular table the rows are products, so the SKUs of variations, which belong to the variation records, are not found. A table that lists variations as rows, as described in show variations as separate rows, finds them.
- Spaces and dashes count. ZDUP112 does not match ZDUP 112; if shoppers type codes in different ways, keep SKUs in one format.
A search field on other pages: [woot_drop_down]
To put a product search into the header, a sidebar or the shop page, use [woot_drop_down]. It shows a search input, and when the shopper presses Enter, a table with the results drops down under it:
[woot_drop_down id=1 placeholder='Search products' width='100%' height=400]
id is the table whose columns the results use, height the maximum height of the drop-down in pixels, and skin sets the look of the results; without it they use the default look, not the table's skin. It searches titles unless the table gets woot_text_search_by through the code above. Placement examples are in how to add a search input with a drop-down table.
Text search with the HUSKY filter
When the table is connected to the HUSKY products filter with filter_provider='woof', as described in connect the HUSKY filter to a WooCommerce product table, its text search can serve as the search field. Enable Search by text on the HUSKY Structure tab (or place [woof_text_filter]) and, in its additional options, set Behavior to the fields to search, for example title or content or excerpt; +SKU adds the SKU to the same field. It differs from the table's own search in a useful way: it splits the text into words and finds products that contain all of them in any order, so shirt red also finds "Red cotton shirt". It also has autocomplete and can search custom fields. Use it instead of the table's own field: once HUSKY is the filter provider, the table passes the filtering to HUSKY and its search by title field no longer narrows the results, so switch off Show search by title for that table.
Speed on large catalogs
Both searches use LIKE with the text in the middle of a value, which the database cannot answer from an index: every search reads the searched fields of all products. Titles are short, so this is fast even with thousands of products. Descriptions are often long, and searching post_content on a large catalog costs noticeably more. Search descriptions only in tables that need it, and keep Min symbols count at 3 or more so that one or two letters do not start a search through every description.
If the search still does not work
- Press Enter after typing, and check that the text is at least Min symbols count characters long.
- Descriptions are not searched: look for spaces or typographic quotes in
woot_text_search_by. - Every search returns all products: the attribute contains no valid field name.
- The shortcode has
mode='json'ormode='to_json': such tables load all rows at once and search them in the browser by title only, without the filter form, sowoot_text_search_byhas no effect. Remove the attribute to search on the server. - The field is missing: Show search by title is off, or the shortcode has
hide_text_search=1.
If none of this helps, open a topic on the WOOT support forum with a link to the page and the shortcode you use.