Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Created July 8, 2024 13:39
Show Gist options
  • Save Acephalia/ed098189a7448617058dbd213696acab to your computer and use it in GitHub Desktop.
Save Acephalia/ed098189a7448617058dbd213696acab to your computer and use it in GitHub Desktop.
Extend WooCommerce search results to include tags, descriptions and price
/*
* @snippet Extend WooCommerce search to include tags, descriptions and price
* @author u/acephaliax
* @source https://insomniainc.com/resources/code-snippets/woocommerce/how-to-extend-woocommerce-search-results-to-show-product-tags-descriptions-sku-and-price/
* @compatibility Last tested on WooCommerce 9.0.1
* @community r/wordpress, r/woocommerce
* @caffeinate https://buymeacoffee.com/acephaliax
*/
add_filter('posts_search', 'ii_extend_woo_search', 10, 2);
function ii_extend_woo_search($searchSql, $query = false) {
// Check if in admin, if not a WP_Query, or if not a search query, then return the default search SQL
if (is_admin() || !is_a($query, 'WP_Query') || !$query->is_search) {
return $searchSql;
}
// Only modify the search SQL if it exists
if ($searchSql) {
global $wpdb;
// Include product tags in the search
$searchSql = preg_replace(
'/ (AND|OR) \\('.preg_quote($wpdb->posts).'\\.post_content (NOT )?LIKE \'(.+)\'\\)/U',
'$0 $1 $2 EXISTS( SELECT 1 FROM '.$wpdb->term_relationships.' JOIN '.$wpdb->term_taxonomy.' USING (term_taxonomy_id) JOIN '.$wpdb->terms.' USING (term_id) WHERE object_id='.$wpdb->posts.'.ID AND taxonomy="product_tag" AND name LIKE \'$3\')',
$searchSql
);
// Include product description, short description, and price in the search
$searchSql .= " OR EXISTS (SELECT 1 FROM {$wpdb->postmeta} WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID AND ";
$searchSql .= "(meta_key = '_price' AND meta_value LIKE '%" . esc_sql($query->query_vars['s']) . "%' ";
$searchSql .= "OR meta_key = '_regular_price' AND meta_value LIKE '%" . esc_sql($query->query_vars['s']) . "%' ";
$searchSql .= "OR meta_key = '_sale_price' AND meta_value LIKE '%" . esc_sql($query->query_vars['s']) . "%' ";
$searchSql .= "OR meta_key = '_sku' AND meta_value LIKE '%" . esc_sql($query->query_vars['s']) . "%' ";
$searchSql .= "OR meta_key = '_description' AND meta_value LIKE '%" . esc_sql($query->query_vars['s']) . "%' ";
$searchSql .= "OR meta_key = '_short_description' AND meta_value LIKE '%" . esc_sql($query->query_vars['s']) . "%'))";
}
return $searchSql;
}
add_action('wp_loaded', 'ii_remove_default_search');
function ii_remove_default_search() {
// Optionally remove any other default search modifications here if needed
add_action('pre_get_posts', 'ii_custom_search');
}
function ii_custom_search($query = false) {
// Check if in admin, if not a WP_Query, or if not a search query, then return
if (is_admin() || !is_a($query, 'WP_Query') || !$query->is_search) {
return;
}
// Apply to all searches
$postTypes = array('post', 'page', 'product');
$query->set('post_type', $postTypes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment