I have a problem with entering a search by product ean number in the administrator panel. My code is:
function custom_search_by_ean( $query ) {
global $pagenow, $typenow;
if ( is_admin() && $pagenow === 'edit.php' && $typenow === 'product' && isset( $_GET['s'] ) && ! empty( $_GET['s'] ) ) {
$ean = $_GET['s'];
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_ean',
'value' => $ean,
'compare' => 'LIKE'
)
);
$query->set( 'meta_query', $meta_query );
error_log(print_r($query, true));
}
}
add_action( 'pre_get_posts', 'custom_search_by_ean' );
I changed the key to see if it would work. For example, key => ‘_sku’ works. Does anyone have an idea what the problem is?
I would like products to be searched in WooCommerce by ean, name, sku, id in the admin panel in the edit.php tab
2
Answers
I found a solution to this problem, I first extract the product ID that is searched for by EAN, and then using the 'post__in' parameter I get the result:
Your code is almost true but there are a couple of issues:
Updated version of your code:
Note: Make sure that the custom field storing the EAN is named ‘_ean’ and that it contains the EAN data for the products.