Search product by title and short description
By default, the Phone Orders product search looks only at the product name. If a customer vaguely remembers a product’s short description but not its title, your agent cannot find it. This guide provides a code snippet that expands the search to also match terms inside the product’s short description. The search becomes more flexible, helping agents locate items faster during a live call.
The Complete Code Snippet
Copy and paste the following PHP code into your child theme’s functions.php file or via the Code Snippets plugin.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
add_filter( "wpo_custom_product_search", function($do_custom_search,$query_args,$term) { //don't do custom search , just set filter add_filter("posts_where", function($where,$query) { global $wpdb; $custom_conditions = array(); foreach($query->query_vars['search_terms'] as $term) { $like = "%" . $wpdb->esc_like( $term ) . "%"; $custom_conditions[] = "CONCAT({$wpdb->posts}.post_title,{$wpdb->posts}.post_excerpt) LIKE '$like'"; } $custom_where = join( " AND ", $custom_conditions); return $where. " OR " . $custom_where; }, 10, 2); return false; }, 10, 3); |
What the Code Does
The code works by extending WooCommerce’s main product query. It does not create a second, independent search. Instead, it adds an extra condition to the existing database query.
| Element | Explanation |
|---|---|
add_filter( "wpo_custom_product_search", ... ) | Hooks into Phone Orders’ custom product search filter. The plugin calls this filter when an agent types a search term. |
return false | Tells the plugin to continue using its default search logic. We are not replacing the search; we are extending it. |
add_filter( "posts_where", ... ) | Adds a filter to the main WooCommerce product query. This filter modifies the WHERE clause of the SQL statement that retrieves products. |
foreach ( $query->query_vars['search_terms'] as $term ) | Loops through each word the agent typed. The plugin has already split the search string into separate terms. |
$wpdb->esc_like( $term ) | Escapes any special SQL characters in the search term (e.g., %, _). This prevents SQL injection and ensures the term is treated as a literal string. |
CONCAT({$wpdb->posts}.post_title,{$wpdb->posts}.post_excerpt) LIKE '$like' | Creates a condition that checks whether the search term appears anywhere inside the concatenated string of the product title and its short description. |
$custom_where = join( " AND ", $custom_conditions ); | Joins multiple conditions with AND. If the agent typed three words, the product must match all three in its title or short description. |
return $where . " OR " . $custom_where; | Appends the new condition to the existing WHEREclause. The search now returns products that match the original conditions (title only) or the new condition (title + short description). |
Step‑by‑Step Implementation
Step 1: Choose where to add the code
- Option A (Recommended): Use the free Code Snippets plugin. Install it from Plugins → Add Newby searching for “Code Snippets”. This method is safe and allows you to activate or deactivate the snippet without touching your theme files.
- Option B: Add the code to your child theme’s
functions.phpfile. Navigate to Appearance → Theme File Editor, openfunctions.php, paste the code at the bottom, and save. Never add custom code directly to a parent theme.
Step 2: Insert the snippet
- If using Code Snippets:
Step 3: Test the functionality
- Open a phone order (WooCommerce → Phone Orders).
- In the product search field, type a word that appears only in a product’s short description, not in its title.
- The product should now appear in the search results.
Customisation Options
You can easily adapt the code to search additional fields.
Include the full product description – Replace post_excerpt (short description) with post_content (long description).CONCAT({$wpdb->posts}.post_title,{$wpdb->posts}.post_content)
Search only the short description (exclude title) – Remove post_title from the CONCATfunction.{$wpdb->posts}.post_excerpt
Search with OR logic (any term, not all terms) – Replace " AND " with " OR " in the joinfunction.$custom_where = join( " OR ", $custom_conditions );
Add more fields (e.g., SKU) – Include additional columns in the CONCAT function.CONCAT({$wpdb->posts}.post_title,{$wpdb->posts}.post_excerpt,{$wpdb->postmeta}.meta_value)You would also need to join the postmeta table, which is more complex. Contact our support team for assistance.