I am trying to extend the ADMIN WooCommerce Product Search to include custom fields (e.g. _sku_2 which I added via functions)
Using this as a guide, I tried to add this code to my functions, but with no luck:
// EXTEND ADMIN PRODUCT SEARCH
add_action( 'pre_get_posts', 'extend_admin_search' );
function extend_admin_search( $query ) {
// Extend search for document post type
$post_type = 'product';
// Custom fields to search for
$custom_fields = array(
"_supplier_sku",
"_sku_2"
);
if( ! is_admin() )
return;
if ( $query->query['post_type'] != $post_type )
return;
$search_term = $query->query_vars['s'];
// Set to empty, otherwise it won't find anything
//$query->query_vars['s'] = '';
if ( $search_term != '' ) {
$meta_query = array( 'relation' => 'OR' );
foreach( $custom_fields as $custom_field ) {
array_push( $meta_query, array(
'key' => $custom_field,
'value' => $search_term,
'compare' => 'LIKE'
));
}
$query->set( 'meta_query', $meta_query );
};
}
It seems not to be editing the query results at all. Is the product search even using the default query?