Skip to content

Instantly share code, notes, and snippets.

@gstreetmedia
Created May 23, 2020 17:37
Show Gist options
  • Select an option

  • Save gstreetmedia/2963d96811d36fc667dba24df464d89b to your computer and use it in GitHub Desktop.

Select an option

Save gstreetmedia/2963d96811d36fc667dba24df464d89b to your computer and use it in GitHub Desktop.
Add _sku search to the product admin
if(is_admin()) {
/**
* @param $join
* @return string
*/
function gstreet_woocommerce_join_sku($join)
{
global $pagenow, $wpdb;
if ($pagenow == 'edit.php' && !empty($_GET['post_type']) && $_GET['post_type'] == 'product' && !empty($_GET['s'])) {
$join .= 'LEFT JOIN ' . $wpdb->postmeta . ' ON ' . $wpdb->posts . '.ID = ' . $wpdb->postmeta . ".post_id AND {$wpdb->postmeta}.meta_key = '_sku' ";
}
return $join;
}
add_filter('posts_join', 'gstreet_woocommerce_join_sku');
/**
* @param $where
* @return string
*/
function gstreet_woocommerce_search_sku($where)
{
global $pagenow, $wpdb;
if ($pagenow == 'edit.php' && !empty($_GET['post_type']) && $_GET['post_type'] == 'product' && !empty($_GET['s'])) {
$where .= " OR {$wpdb->postmeta}.meta_value LIKE '{$_GET['s']}'";
}
return $where;
}
add_filter('posts_where', 'gstreet_woocommerce_search_sku');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment