daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Oct 18th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //Do NOT include the opening php tag
  3.  
  4. //Product search by SKU
  5. function xr_join_product_search( $join, $query ) {
  6.   if ( ! $query->is_main_query() || is_admin() || ! is_search() || ! is_woocommerce() ) {
  7.     return $join;
  8.   }
  9.   global $wpdb;
  10.   $join .= " LEFT JOIN {$wpdb->postmeta} post_meta ON {$wpdb->posts}.ID = post_meta.post_id ";
  11.   return $join;
  12. }
  13. add_filter( 'posts_join', 'xr_join_product_search', 10, 2 );
  14.  
  15. function xr_where_product_search( $where, $query ) {
  16.   if ( ! $query->is_main_query() || is_admin() || ! is_search() || ! is_woocommerce() ) {
  17.     return $where;
  18.   }
  19.   global $wpdb;
  20.   $where = preg_replace(
  21.     "/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
  22.     "({$wpdb->posts}.post_title LIKE $1) OR (post_meta.meta_key = '_sku' AND post_meta.meta_value LIKE $1)", $where
  23.   );
  24.   return $where;
  25. }
  26. add_filter( 'posts_where', 'xr_where_product_search', 10, 2 );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top