Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('posts_search', 'product_search_add_sku',8);
- function product_search_add_sku($where) {
- global $pagenow, $wpdb, $wp;
- if ((is_admin() && 'edit.php' != $pagenow)
- || !is_search()
- || !isset($wp->query_vars['s'])
- || (isset($wp->query_vars['post_type']) && 'product' != $wp->query_vars['post_type'])
- || (isset($wp->query_vars['post_type']) && is_array($wp->query_vars['post_type']) && !in_array('product', $wp->query_vars['post_type']) )
- ) {
- return $where;
- }
- $search_ids = array();
- $terms = explode(',', $wp->query_vars['s']);
- foreach ($terms as $term) {
- if (is_admin() && is_numeric($term)) {
- $search_ids[] = $term;
- }
- $sku_to_parent_id = $wpdb->get_col($wpdb->prepare("SELECT p.post_parent as post_id FROM {$wpdb->posts} as p join {$wpdb->postmeta} pm on p.ID = pm.post_id and pm.meta_key='_sku' and pm.meta_value LIKE '%%%s%%' where p.post_parent <> 0 group by p.post_parent", wc_clean($term)));
- $sku_to_id = $wpdb->get_col($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_sku' AND meta_value LIKE '%%%s%%';", wc_clean($term)));
- $search_ids = array_merge($search_ids, $sku_to_id, $sku_to_parent_id);
- }
- $search_ids = array_filter(array_map('absint', $search_ids));
- if (sizeof($search_ids) > 0) {
- $where = str_replace(')))', ") OR ({$wpdb->posts}.ID IN (" . implode(',', $search_ids) . "))))", $where);
- }
- return $where;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement