0

I have some problem with admin panel product search. The products in some categories can't be found by SKU, only by name, while the others can. I've tried to search with SKU:, but that didn't change anything. WordPress v5.1.1, WooCommerce v3.6.4

1

Add This Code In function.php then save it.

After Changes, You can see the changes in the screenshots

enter image description here

enter image description here

function search_by_sku_for_admin( $search, &$query_vars ) {
    global $wpdb, $pagenow;

    if ( 'edit.php' != $pagenow || empty($search) ) {
        return $search;
    }

    $args = array(
        'posts_per_page'  => -1,
        'post_type'       => 'product',
        'meta_query' => array(
            array(
                'key' => '_sku',
                'value' => $query_vars->query['s'],
                'compare' => 'LIKE'
            )
        )
    );
    $posts = get_posts( $args );
    if ( empty( $posts ) ) return $search;
    $get_post_ids = array();
    foreach($posts as $post){
        $get_post_ids[] = $post->ID;
    }
    if ( sizeof( $get_post_ids ) > 0 ) {
        $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
    }
    return $search;
}
add_filter( 'posts_search', 'search_by_sku_for_admin', 999, 2 );
New contributor
Rohit Verma is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.